从 PHAssets Array 请求图像导致内存异常

Posted

技术标签:

【中文标题】从 PHAssets Array 请求图像导致内存异常【英文标题】:Requesting Image from PHAssets Array causes in memory exception 【发布时间】:2016-06-07 13:14:37 【问题描述】:

我正在使用此代码使用照片框架从相册中检索照片, 我已成功检索 PHAsset 并将其放入数组中,但我无法将其转换为图像,它在此过程中崩溃。

这是我的代码,

-(void)retrieveAllPhotoSetWithCompletionCallback:(void(^ __nonnull)(NSMutableArray * __nullable asset ,NSMutableArray * __nullable imageArray , BOOL complete))completionCallback

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

    NSMutableArray *assetsArray = [NSMutableArray new];
    PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
    [fetchResult enumerateObjectsUsingBlock:^(PHAsset *_Nonnull asset, NSUInteger idx, BOOL * _Nonnull stop) 

        [assetsArray addObject:asset];
    ];

    PHImageManager *manager = [PHImageManager defaultManager];
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[assetsArray count]];

    // assets contains PHAsset objects.
//    __block UIImage *ima;
    int i = 0;

    for(PHAsset *asset in assetsArray) 
        // This autorelease pool seems good (a1)
        @autoreleasepool 

            [manager requestImageForAsset:asset
                               targetSize:PHImageManagerMaximumSize
                              contentMode:PHImageContentModeDefault
                                  options:self.requestOptions
                            resultHandler:^(UIImage *image, NSDictionary *info) 

                                dispatch_async(dispatch_get_main_queue(), ^

                        //you can add autorelease pool here as well (a2)
                    @autoreleasepool 

                        //assetCount++;
                        NSLog(@"started requesting image %i", i);

                        NSError *error = [info objectForKey:PHImageErrorKey];

                        if (error)
                            NSLog(@"Image request error: %@",error);
                        else 
                            NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%i.png",i]];
                            NSData *imageData = UIImagePNGRepresentation(image);
                            if(imageData) 
                                [imageData writeToFile:imagePath atomically:YES];
                                [images addObject:imagePath];
                            
                            else 
                                NSLog(@"Couldn't write image data to file.");
                            
                            NSLog(@"finished requesting image %i", i);
                        
                     //a2 ends here
                );
            ];
            i++;
         // a1 ends here
    
    completionCallback(assetsArray,images,YES);

【问题讨论】:

【参考方案1】:

我认为问题出在requestImageForAsset:asset targetSize:PHImageManagerMaximumSize 上,您正在使用程序中可用的最大尺寸。这导致将最大尺寸的图像存储在 RAM 中。通过存储多个它会在 RAM 中消耗更多内存并且它会崩溃。我调整了 targetSize 的大小,内存使用量比以前少了。

【讨论】:

以上是关于从 PHAssets Array 请求图像导致内存异常的主要内容,如果未能解决你的问题,请参考以下文章

使用 PHAssets 时出现此错误:Creating an image format with an unknown type is an error

CGDataProviderCopyData 在内存中堆积导致崩溃

如何在 PHAsset 中添加图像并将其从照片库中删除?

将 PNG 图像保存到内存流会导致错误

凌空请求的匿名侦听器导致内存泄漏

AlamoFire 图像内存问题