objective-c - CGImageRelease 导致崩溃

标签 objective-c ios uiimage

我正在使用 AGImagePickerController 从相册中挑选多张图片,然后将所选 Assets 推送到 viewController,它会尝试将每个 Assets 转换为 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 之后,内存警告消失了。但是,在最后一个 Assets 转换为 UIImage 后,我的应用程序将崩溃。到目前为止,我无法找出它崩溃的原因。

最佳答案

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

关于objective-c - CGImageRelease 导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13279101/

相关文章:

ios - 调用方法时出现奇怪的错误(UITextSelectionView)?

ios - 动态改变 fetchedResultsController

ios - RestKit:连接多对多关系

ios - RxSwift 中 RxJava onErrorResumeNext 运算符的模拟是什么?

objective-c - iOS从nsdata压缩图片

objective-c - 关于applescript和expose的问题

iOS,如何将 .xib 加载到另一个 .xib 中?

iphone - 页面控制问题

ios - 将 UIBezierPath 转换为 UIImage

iphone - Howe 捕获 UIView 顶部 UIView