ios - 如何获取所选图像的 'file' 的名称

标签 ios iphone image uiimageview

我正在尝试获取使用此代码选择的图像的文件名:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    img = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.imgFoto setImage:img];

    NSURL *imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
    NSString *imageName = [imagePath lastPathComponent];
    NSLog(@"image name is %@", imageName);

    [self dismissViewControllerAnimated:YES completion:NULL];
}

但我看到的是:

enter image description here

我的意思是,我不应该像 IMG_00001.jpegIMG_00002.jpeg 等等?

问候。

最佳答案

按以下代码行使用 AssetsLibrary

#import <AssetsLibrary/AssetsLibrary.h>

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

 NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];

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
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];

关于ios - 如何获取所选图像的 'file' 的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613690/

相关文章:

iphone - PlaySystemSound 静音开关打开

ios - 从 iPhone 加载联系人在 Swift 中崩溃

ios - 从自定义 UITableViewCell 更新数据

ios - 如何直接从 URL 在 swift 中打开 pdf 文件

ios - 如何对齐 UIImage 下面的 UIButton 中的文本,因为它没有正确显示

iphone - 应用程序在设备上崩溃,但 XCode 未记录崩溃

html - Bootstrap 轮播从底部展开图像

image - 以 'life' 尺寸显示图像而不仅仅是全尺寸?

javascript - 如何检查已加载的图像是否已加载到 JavaScript 中?

android - React Native 持续集成(支持 iOS & Android)