ios - 如何使用文件路径的引用获取图库文件路径并显示图像

标签 ios objective-c iphone

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

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

}

使用这个委托(delegate)如何获取图像文件路径并在另一个 View 中显示此图像

最佳答案

在图像文件路径中,您将获得 Assets 的 url,您可以使用该 Assets url 在 ImageView 中显示图像。

首先导入

#import <AssetsLibrary/AssetsLibrary.h>

在你的项目中,然后在 UIImagePicker 的委托(delegate)方法中

- (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"];

关于ios - 如何使用文件路径的引用获取图库文件路径并显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393957/

相关文章:

c# - Xamarin.Forms Shell GoToAsync 在 iOS 中无法按预期工作

c++ - 为什么比较无符号表达式 < 0 总是假的

iphone - UITableView 类似于 gridView

iphone - QLPreviewController View

iOS 8 iPad : Could not load NIB in bundle

ios - 如何在 Xcode 12(.5) 中设置基本本地化和开发语言?

objective-c - 使用 Core Graphics 强制窗口重绘自身?

ios - NSMutableArray 索引 9223372036854775807 越界

c# - 有没有办法在 C# 中为 iphone 制作应用程序

ios - 如何生成签名方法OAuth1