如何处理大尺寸的多张图像? [英] How to handle multiple images with large size?

查看:140
本文介绍了如何处理大尺寸的多张图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从照片"库中选择的图像的列表. 阵列列表包含照片"中的选定PHAssets

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 "

现在我想将这些图像存储到文档目录中.为此,我将PHAssets转换为原始图像并将其存储在数组中,对于我执行的代码

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

                        }]; 
}

但是问题是,当我选择多张大尺寸(大于25)的图像时,应用崩溃了.因为当我通过使用PHImageManager将PHAsset转换为原始图像时,它会占用设备的大量内存. 请帮我解决这个问题.如果有其他解决方案来处理用于将图像存储在应用程序文档目录中的选定PHAsset,请提供我.

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.

推荐答案

 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.

这篇关于如何处理大尺寸的多张图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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