如何使用文件pathe参考获取图库文件路径和显示图像

时间:2015-09-04 08:53:17

标签: ios objective-c iphone

我想在关闭此视图后获取Gallery图像文件路径并保存此文件路径,并打开另一个视图,使用此文件路径引用显示此图像...

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{ 
NSLog(@"%@",info);

}

使用此委托如何获取图像文件路径并将此图像显示为另一个视图

1 个答案:

答案 0 :(得分:2)

在图像文件路径中,您将获得资产的网址,您可以使用该资产网址在图片视图上显示图片。

首次导入

#import <AssetsLibrary/AssetsLibrary.h>

在您的项目中,然后在UIImagePicker

的委托方法中
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
  // You will get the image url like this
  NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
}

现在要显示图像,请使用此代码

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        CGImageRef iref = [rep fullResolutionImage];
        if (iref) {
            //here you get the image
            largeimage = [UIImage imageWithCGImage:iref];
        }
    };

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

如果您想直接访问该图片,请使用此

UIImage* myImage=(UIImage*)[info objectForKey:@"UIImagePickerControllerOriginalImage"];