1

我正在使用 AGImagePickerController 从相册中挑选多张图片,然后将选定的资产推送到视图控制器,在那里它尝试将每个资产转换为 UIImage。但是,我发现如果我选择了超过 20 张图像,我将开始收到内存不足警告并退出应用程序。这是我的转换代码

for(int i =0 ; i < [self.selectedPictures count] ; i++)
{
    NSLog(@"Object %d",i);
    ALAsset *asset = [self.selectedPictures objectAtIndex:i];
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    UIImage *anImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
    float newHeight = anImage.size.height / (anImage.size.width / 1280);

    UIImage *resizedImage = [anImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(newHeight, 1280.f) interpolationQuality:kCGInterpolationHigh];

    UIImage *resizedThumbnailImage = [anImage resizedImageWithContentMode:UIViewContentModeScaleAspectFill bounds:CGSizeMake(290.0f, 300.f) interpolationQuality:kCGInterpolationHigh];

    // JPEG to decrease file size and enable faster uploads & downloads
    NSData *imageData = UIImageJPEGRepresentation(resizedImage, 0.6f);
    //NSData *thumbnailImageData = UIImagePNGRepresentation(thumbnailImage);
    NSData *thumbnailImageData = UIImageJPEGRepresentation(resizedThumbnailImage, 0.6f);


    PFFile *photoFile = [PFFile fileWithData:imageData];
    PFFile *thumbnailFile = [PFFile fileWithData:thumbnailImageData];

    [photoFile saveinbackground];
    [thumbnailFile saveinbackground];
}

所以我想我应该添加 CGImageRelease(iref); 在 anImage 释放 iref 之后,内存警告就消失了。但是,在最后一个资产转换为 UIImage 后,我的应用程序将崩溃。到目前为止,我无法找出它崩溃的原因。

4

2 回答 2

3

你不应该这样做CGImageRelease(iref);,除非你使用CGImageCreate, CGImageCreateCopyCGImageRetain. 这就是它崩溃的原因。

于 2012-11-07T22:09:27.703 回答
-1

我找到了解决这个问题的方法。采用@autoreleasepool

于 2012-11-07T23:29:35.403 回答