如何获取UIImagePicker所选的图像名称?

use*_*065 3 image uiimagepickercontroller ios

有人知道如何在呈现UIImagePickerController后获取所选图像名称(来自照片库的图像名称)吗?

Ara*_*han 10

ALAsset的帮助下,您可以实现这一目标.

第1步:Asset从中获取所选图像UIImagePicker

第2步:获取ALAssetRepresentationAsset.

第3步:ALAssetRepresentation类有一个名为的属性fileName

- (NSString *)filename
Run Code Online (Sandbox Code Playgroud)

返回表示磁盘上表示的文件名的字符串.

此示例代码将帮助您......

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

    NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *representation = [myasset defaultRepresentation];
        NSString *fileName = [representation filename];
        NSLog(@"fileName : %@",fileName);
    };

    ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
    [assetslibrary assetForURL:imageURL 
                   resultBlock:resultblock
                  failureBlock:nil];

}
Run Code Online (Sandbox Code Playgroud)

注意:它仅适用于iOS 5及更高版本.