ios – 如何在Objective-C中将PHAsset转换为UIImage

前端之家收集整理的这篇文章主要介绍了ios – 如何在Objective-C中将PHAsset转换为UIImage前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个填充有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,creationDate=2015-07-27 02:02:47 +0000,favorite=0 
)

图像为空.有人知道如何添加将PHAssets转换成UIImage并将它们添加到images数组?任何帮助是赞赏.谢谢!

解决方法

对于那个在这个问题上努力的人来说,这是要走的路.

首先将requestOptions设置为:

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

// this one is key
self.requestOptions.synchronous = true;

并且如果数组中有多个资源填充有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];
                    }];


}

现在图像阵列包含uiimage格式的所有图像.

原文链接:https://www.f2er.com/iOS/329618.html

猜你在找的iOS相关文章