How to convert PHAsset to UIImage in Objective-C

后端 未结 4 2021
野的像风
野的像风 2021-01-31 21:37

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

4条回答
  •  梦谈多话
    2021-01-31 22:20

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

    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 = YES;
    

    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];
                        }];
    
    
    }
    

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

提交回复
热议问题