-->

How to handle multiple images with large size?

2019-09-09 23:06发布

问题:

I have list of selected images from Photos gallery. The array list contain selected PHAssets from Photos

 <PHAsset: 0x131334350> 768AD8BA-FE7A-4EE9-B2CF-4DD4AEBE2AE9/L0/001 mediaType=1/0, sourceType=1, (2448x3264), creationDate=2016-03-11 06:50:09 +0000, location=1, hidden=0, favorite=0 ",
"<PHAsset: 0x131334260> CA962E42-A367-4E8D-85C8-934F8C76FE78/L0/001 mediaType=1/0, sourceType=1, (2448x3264), creationDate=2016-03-11 06:50:08 +0000, location=1, hidden=0, favorite=0 "

Now i want to store this images into Document Directory.For that i am converting PHAssets to original image and store it in array, for that code i did

  @property (nonatomic, strong) PHImageRequestOptions *requestOptions;

  for (PHAsset *asset in assetArray) {
      self.requestOptions = [[PHImageRequestOptions alloc] init];
      self.requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
      self.requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;

    // this one is key
    self.requestOptions.synchronous = true;
    PHImageManager *manager = [PHImageManager defaultManager];
    __block UIImage *ima;
        // Do something with the asset
        [manager requestImageForAsset:asset
                           targetSize:PHImageManagerMaximumSize
                          contentMode:PHImageContentModeDefault
                              options:self.requestOptions
                        resultHandler:^void(UIImage *image, NSDictionary *info) {
                            ima = image;

                            [Albumimages addObject:ima];

                        }]; 
}

But problem is that when i selected multiple images with large size(more than 25) app is crash down. Because when i convert PHAsset to original image by using PHImageManager it uses high memory of device. Please help me to solve this problem. Provide me if there is any other solution to handle selected PHAssets for storing images in application document directory.

回答1:

 PHImageRequestOptions *option = [PHImageRequestOptions new];
 option.synchronous = NO;

let screenSize: CGSize = UIScreen.mainScreen().bounds.size
let targetSize = CGSizeMake(screenSize.width, screenSize.height)

 [manager requestImageForAsset:asset
                    targetSize:target 
                   contentMode:PHImageContentModeAspectFill 
  options:option resultHandler:^(UIImage *result, NSDictionary *info) 
  {
        //this block will be called synchronously
   }];

If you set maximum image size when using more images they could crash but if set target size then handles it.



回答2:

Handle all images with the data instead of covert into original image.

     [manager requestImageDataForAsset:asset
                              options:self.requestOptions
                        resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info)
     {
        // UIImage *image = [UIImage imageWithData:imageData];
         [Albumimages addObject:imageData];

     }];

After that Write image data at document directory path.