【问题标题】:How to get image gallery file path and display image using reference of file pathe如何使用文件路径的引用获取图库文件路径并显示图像
【发布时间】:2015-11-30 08:53:09
【问题描述】:

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

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

}

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

【问题讨论】:

  • 检查 (stackoverflow.com/questions/7777282/…)。它将显示如何获取所选图像的 URL。
  • 感谢您的回复解决了我的问题,但我想打开相机并单击(使用此图像)选项后单击图像未获取图像路径不显示图像如何解决此问题
  • 为此,您应该先使用ALAssetLibrary 保存图像,然后才能获得点击图像和路径。

标签: ios objective-c iphone


【解决方案1】:

在图片文件路径中你会得到asset的url,你可以使用那个asset url来在image view上显示图片。

第一次导入

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

【讨论】:

  • 感谢 Rajatp 对上述代码的回复解决了我的问题,但我想打开相机并在单击图像后单击(使用此图像)选项无法获取图像路径如何解决此问题
  • 您不会从相机获取图像路径,如果您想将该图像保存到图库,那么您将从资产中获取路径。
  • 要做到这一点,请检查此链接 - stackoverflow.com/questions/4457904/…
  • 点击图片后如何将图片保存到图库中
猜你喜欢
  • 2016-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 2012-04-29
相关资源
最近更新 更多