【问题标题】:How to convert PHAsset to UIImage in Objective-C如何在 Objective-C 中将 PHAsset 转换为 UIImage
【发布时间】:2015-07-28 08:20:20
【问题描述】:

我有一个充满PHAsset 对象(https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAsset_Class/index.html)的数组,我想知道如何将它们转换为 UIImage,然后将它们保存在数组中。

包含 PHAsset 对象的数组称为 self.assets,这是我目前所拥有的:

PHImageManager *manager = [PHImageManager defaultManager];

CGFloat scale = UIScreen.mainScreen.scale;

NSMutableArray *images = [NSMutableArray arrayWithCapacity:[self.assets count]];

for (int i = 0; i < [self.assets count]; i++) {

    CGSize targetSize = CGSizeMake(scale, scale);

    [manager requestImageForAsset:[self.assets objectAtIndex:i]
                       targetSize:targetSize
                      contentMode:PHImageContentModeAspectFill
                          options:self.requestOptions
                    resultHandler:^(UIImage *image, NSDictionary *info){
                        [images addObject:image];
                    }];
}

self.requestOptions 是 .h 中的一个属性

@property (nonatomic, strong) PHImageRequestOptions *requestOptions;

在 viewDidLoad 我正在这样做:

self.requestOptions = [[PHImageRequestOptions alloc] init];
self.requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
self.requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

但在进行一些调试后,我一直看到self.assets 具有以下值:

(
<PHAsset: 0x1743828a0> 3B6D658D-EC76-43A1-9793-35D889E9CF15/L0/001 mediaType=1/0, assetSource=3, (2448x2448), creationDate=2015-07-27 02:02:46 +0000, location=1, hidden=0, favorite=0 ,
<PHAsset: 0x174382970> 50F05575-71D2-446B-BD1E-8E3250E375AD/L0/001 mediaType=1/0, assetSource=3, (2448x2448), creationDate=2015-07-27 02:02:47 +0000, location=1, hidden=0, favorite=0 
)

images 为空。有谁知道我如何将 PHAssets 添加到 UIImages 并将它们添加到 images 数组中?任何帮助表示赞赏。谢谢!

【问题讨论】:

    标签: ios objective-c iphone arrays uiimage


    【解决方案1】:

    对于任何像我一样在这个问题上苦苦挣扎的人,这是要走的路。

    首先将requestOptions设置为:

    self.requestOptions = [[PHImageRequestOptions alloc] init];
    self.requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
    self.requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    
    // this one is key
    self.requestOptions.synchronous = YES;
    

    如果在一个填充有 PHAsset 对象的数组中有多个资产,则添加以下代码:

    self.assets = [NSMutableArray arrayWithArray:assets];
    PHImageManager *manager = [PHImageManager defaultManager];
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[assets count]];
    
    // assets contains PHAsset objects.
     __block UIImage *ima;
    
    for (PHAsset *asset in self.assets) {
        // Do something with the asset
    
        [manager requestImageForAsset:asset
                           targetSize:PHImageManagerMaximumSize
                          contentMode:PHImageContentModeDefault
                              options:self.requestOptions
                        resultHandler:^void(UIImage *image, NSDictionary *info) {
                            ima = image;
    
                            [images addObject:ima];
                        }];
    
    
    }
    

    现在images 数组包含uiimage 格式的所有图像。

    【讨论】:

    • 此代码无效!通过 resultHandler 返回的 UIImage *image 始终为 nil。
    • 给这个人一块饼干。 requestOptions.synchronous = YES;这解决了我在请求资产时的神秘错误。 [Generic] Creating an image format with an unknown type is an error
    【解决方案2】:

    Swift 3.0 答案

    let photoAsset = asset
            let manager = PHImageManager.default()
            var options: PHImageRequestOptions?
            options = PHImageRequestOptions()
            options?.resizeMode = .exact
            options?.isSynchronous = true
            manager.requestImage(
                for: photoAsset,
                targetSize: PHImageManagerMaximumSize,
                contentMode: .aspectFill,
                options: options
            ) { [weak self] result, _ in
                completion(result)
            }
    

    options?.isSynchronous = true很重要

    BOOL synchronous; // return only a single result, blocking until available (or failure). Defaults to NO
    

    【讨论】:

    • ** isSynchronous** 如果为 false(默认值),则 requestImage(for:targetSize:contentMode:options:resultHandler:) 方法会立即返回。根据 deliveryMode 属性,Photos 可能会在方法返回之前调用您的 resultHandler 块,在稍后的某个时间,或两者兼而有之。如果为 true,则 requestImage(for:targetSize:contentMode:options:resultHandler:) 方法会阻塞调用线程,直到图像数据准备好或发生错误。照片只调用一次结果处理程序块。 >注意仅从后台线程执行同步请求。
    • 如何在Objective-C中将PHAsset转换为UIImage
    • 嘿@JamesWolfe 你可以看到Shalin Shah 的上述回答。它在objective-c中
    【解决方案3】:

    图像可能为零(很少)。最好评估一下,否则array add nil会导致app崩溃。

    if(image){
        ima = image;
        [images addObject:ima];
    }
    

    【讨论】:

    • 这个答案的字面意思是 0 上下文,只是检查未知变量是否为零的随机代码。
    猜你喜欢
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2012-05-02
    • 2017-01-06
    • 2018-03-23
    • 1970-01-01
    • 2019-02-15
    • 2016-11-12
    相关资源
    最近更新 更多