如何获取所选图像的“文件名”

时间:2014-02-06 20:46:08

标签: 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等?

问候。

1 个答案:

答案 0 :(得分:1)

使用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];