gpt4 book ai didi

objective-c - CGImageRelease 导致崩溃

转载 作者:行者123 更新时间:2023-11-29 11:06:42 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com