如何在目标c中的UILabel上显示UIImage文件路径

Posted

技术标签:

【中文标题】如何在目标c中的UILabel上显示UIImage文件路径【英文标题】:how to show UIImage file path on UILabel in objective c 【发布时间】:2017-03-30 11:07:57 【问题描述】:

我是 ios 新手,在 UILabel 上显示文件路径时遇到问题。我正在为画廊拍摄图片,我想在 UILabel 上显示图片名称及其扩展名。

我的代码是这样的

按钮点击...

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:nil]; 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo

    imgPanCard.image = image;
    [picker dismissModalViewControllerAnimated:YES];

我需要在 UILabel "No. File Chosen" 上显示图像名称及其扩展名 如何执行此操作。在此先感谢!

【问题讨论】:

查看***.com/questions/4314405/… 试试这个***.com/a/20251513 @RajeshkumarR 我试试你的代码,但是 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 方法在调用此方法后没有被调用 didFinishPickingMediaWithInfo @RajeshkumarR 你在吗??? 【参考方案1】:

我只是像这样更改代码

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

        NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];

        // define the block to call when we get the asset based on the url (below)
        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
        
            ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
            NSLog(@"[imageRep filename] : %@", [imageRep filename]);
            lblPanCard.text=[imageRep filename];
            [picker dismissViewControllerAnimated:NO completion:nil];
            imgPanCard.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        ;

        // get the asset library and fetch the asset based on the ref url (pass in block above)
        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
        [assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];


【讨论】:

【参考方案2】:

在“didFinishPickingMediaWithInfo”方法中添加如下代码:

// get the ref url
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];

// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)

    ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
    NSLog(@"[imageRep filename] : %@", [imageRep filename]);
;

// get the asset library and fetch the asset based on the ref url (pass in block above)
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];

【讨论】:

如果答案可用,不要复制粘贴答案到另一个问题,而不是在评论框中分享链接 @HirenPatel 您的代码给了我图像名称和扩展名,但在从图库中选择图像时选择后它不起作用。 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 方法没有被调用。

以上是关于如何在目标c中的UILabel上显示UIImage文件路径的主要内容,如果未能解决你的问题,请参考以下文章

如何在目标 c 中的视图控制器上获取自定义 TableViewCell 值

自动布局在 x 轴上居中 uilabel 适合内容,uiimage 跟随在左侧

UILabel 的屏幕截图和 UIImageView 中的内容

如何在滚动视图中使 UIImage 低于 UILabel 或 UITextView

如何在目标 c 中裁剪图像?

如果 UITableView 没有数据如何添加 UIImage 和 UILabel