【问题标题】:Load photo from gallery从图库加载照片
【发布时间】:2015-11-09 15:41:35
【问题描述】:

我有一张来自相机胶卷的照片的NSURL,我想从中加载图像或NSData

我试试这个:

NSData *imageDataTemp = [NSData dataWithContentsOfURL:path];

但它返回图像大小的一半。

【问题讨论】:

  • 您发布的代码与图片无关。请使用与图片相关的代码更新您的问题,并进一步解释图片的问题所在。
  • 你是如何从相机胶卷中获取图片的 nsurl 的?
  • 它用于共享扩展。我选择要分享的照片并在我的视图控制器中获取 url

标签: ios image nsurl


【解决方案1】:

这是加载具有ALAsset URL 的图像的方法。

使用 ALAssetLibrary(在 iOS 9.0 中已弃用)

ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *asset)
{
    ALAssetRepresentation *representation = [asset defaultRepresentation];
    CGImageRef image = [representation fullResolutionImage];

    if (image) 
    {
        yourImageView.image = [UIImage imageWithCGImage:image];
    }
};

ALAssetsLibraryAccessFailureBlock failureBlock  = ^(NSError *error)
{
    NSLog(@"Error %@", [error localizedDescription]);
};

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library assetForURL:assetURL 
         resultBlock:resultBlock
        failureBlock:failureBlock];

使用 PHPhotoLibrary

​​>
    NSURL *yourALAssetURL;
    PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[yourALAssetURL] options:nil];
    PHAsset *asset = result.firstObject;

    if (asset)
    {
        PHCachingImageManager *imageManager = [[PHCachingImageManager alloc] init];

        // Request an image for the asset from the PHCachingImageManager.
        [imageManager requestImageForAsset:asset
                                targetSize:CGSizeMake(100.0f, 100.0f)
                               contentMode:PHImageContentModeAspectFill
                                   options:nil
                             resultHandler:^(UIImage *image,
                                             NSDictionary *info)
         {
             NSLog(@"IMAGE: %@", image);
         }];
    }

Apple 创建了一个使用 PHPhotoLibrary 的示例应用。可用here

【讨论】:

  • 我知道 ALAssetLibrary 在 iOS 9.0 中已弃用。替代品是 PHPhotoLibrary,但我找不到使用它的示例
  • 我想通了并更新了我的答案。请检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
相关资源
最近更新 更多