如何转换的UIImage在Objective-C [英] How to convert to UIImage in Objective-C

查看:489
本文介绍了如何转换的UIImage在Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我充满了 PHAsset 一个数组对象(<一个href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/Photos/Reference/PHAsset_Class/index.html\" rel=\"nofollow\">https://developer.apple.com/library/$p$prelease/ios/documentation/Photos/Reference/PHAsset_Class/index.html),我想知道我怎么可以转换成一个UIImage,然后将它们保存在数组中。

I have an array filled with PHAsset objects (https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAsset_Class/index.html), and I want to know how I can convert them into a UIImage and then save them in an Array.

与PHAsset对象的数组被称为 self.assets 这里是我迄今为止:

The array with the PHAsset objects is called self.assets and here is what I have so far:

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

self.requestOptions is a property in the .h

@property (nonatomic, strong) PHImageRequestOptions *requestOptions;

和在viewDidLoad中我这样做:

and in the viewDidLoad I am doing this:

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 
)

图片是空的。有谁知道我如何添加转换成PHAssets和UIImages它们添加到图片数组?任何帮助是AP preciated。谢谢!

and images is empty. Does anyone know how I can add convert the PHAssets into UIImages and add them to the images array? Any help is appreciated. Thanks!

推荐答案

有关,因为我对这个问题苦苦挣扎的人一样多,这是要走的路。

For anyone struggling as much as I had on this issue, this is the way to go.

首先设置requestOptions为:

First set the requestOptions as:

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

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

和是否有在充满PHAsset对象的数组是多个资产,然后添加此code:

and if there are multiple assets in an array filled with PHAsset objects, then add this code:

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 格式的所有图像。

and now the images array contains all the images in uiimage format.

这篇关于如何转换的UIImage在Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆