CGImageDestinationFinalize或UIImageJPEGRepresentation – 在IOS 10上保存大文件时发生崩溃

我正在尝试为更大的图像创build切片,而且从IOS 10开始,下面的代码看起来不再起作用,并且与EXC_BAD_ACCESS一起崩溃。

这只发生在IOS 10设备上,IOS 9工作正常。

任何大于〜1300×1300的图像都会发生崩溃。

仪器分析不会产生任何有趣的信息,并指向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 灰度,你会得到一个崩溃。

我正在处理许多图像,直到后来才点击它可能与图像的颜色模式有关。

我已经提交了一个苹果的错误,看看他们说什么,但同时我不得不find一个临时修复。

我的修复是通过绘制图像并将其捕捉到新图像中,将图像转换为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; } 

希望这有助于某人。

  1. tempimage是你传递给UIImageJPEGRepresentation的参数UIImageJPEGRepresentation ; 如果它指向一个死对象,那么当UIImageJPEGRepresentation尝试使用那个死对象时会导致崩溃。 检查tempImage是否为零。
  2. 在目标处调用CGImageDestinationFinalize之后,检查是否添加图像