[英]CGImageDestinationFinalize or UIImageJPEGRepresentation - Crash when saving a large file on IOS 10
我正在尝试为更大的图像创建切片,似乎从IOS 10开始,以下代码不再有效并且与EXC_BAD_ACCESS崩溃。
这只发生在IOS 10设备上,IOS 9工作正常。
任何大于~1300x1300的图像都会发生崩溃。
仪器中的分析不会产生任何有趣的内容,并指向CGImageDestinationFinalize。
没有记忆飙升。
我试过以下两种方式:
UIImage* tempImage = [UIImage imageWithCGImage:tileImage];
NSData* imageData = UIImageJPEGRepresentation(tempImage, 0.8f); // CRASH HERE.
要么
+ (BOOL) CGImageWriteToFile: (CGImageRef) image andPath: (NSString *)path
{
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeJPEG, 1, NULL);
if (!destination) {
NSLog(@"Failed to create CGImageDestination for %@", path);
return NO;
}
CGImageDestinationAddImage(destination, image, nil);
if (!CGImageDestinationFinalize(destination)) { // CRASH HERE
NSLog(@"Failed to write image to %@", path);
CFRelease(destination);
return NO;
}
CFRelease(destination);
据我了解,UIImageJPEGRepresentation最终会调用CGImageDestinationFinalize。
编辑:忘记提到图像写入硬盘,但大约一半。
这确实与IOS 10有关,这可能是一个错误吗?
为了以防万一,我还用苹果提交了错误。
任何关于如何写出大图像的帮助或解决方法都非常感谢。
我将在这里回答我自己的问题。
我收集到的是IOS 10在保存具有“ 灰度 ”颜色模式的文件时遇到问题。
所以,如果你这样做
UIImageJPEGRepresentation(tempImage, 0.8f)
如果tempImage是灰度级,你将会崩溃。
我正在处理许多图像,直到后来才点击它可能与图像的颜色模式有关。
我已经用苹果提出了一个错误,看看他们说了什么,但同时我必须找到一个临时修复。
我的修复是通过绘制图像并将其捕获到新图像来将图像转换为RGB。
样品方法:
+ (UIImage *)convertImage:(UIImage *)sourceImage
{
UIGraphicsBeginImageContext(sourceImage.size);
[sourceImage drawInRect:CGRectMake( 0, 0, sourceImage.size.width, sourceImage.size.height)];
UIImage *targetImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return targetImage;
}
希望这有助于某人。
tempimage
是你传递给UIImageJPEGRepresentation
的参数UIImageJPEGRepresentation
; 如果它指向一个死对象,那么当UIImageJPEGRepresentation
试图使用该死对象时,这将导致崩溃。 检查tempImage
是否为零。 CGImageDestinationFinalize
后检查是否要添加图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.