CGImageDestinationFinalize或UIImageJPEGRepresentation-在IOS 10上保存大文件时崩溃 [英] CGImageDestinationFinalize or UIImageJPEGRepresentation - Crash when saving a large file on IOS 10
问题描述
我正在尝试为更大的图像创建图块,并且似乎从IOS 10开始,以下代码不再起作用,并因EXC_BAD_ACCESS而崩溃。
I am trying to create tiles for a larger image, and it seems that as of IOS 10 the following code no longer works and crashes with EXC_BAD_ACCESS.
仅在IOS 10设备上会发生这种情况,IOS 9可以正常工作。
This happens on IOS 10 Device only, IOS 9 works fine.
崩溃的任何图像都会发生大于〜1300x1300。
The crash happens with any image that is larger than ~1300x1300.
在仪器中进行配置不会产生任何有趣的结果,并指向CGImageDestinationFinalize。
Profiling in instruments doesn't yield anything interesting and points to CGImageDestinationFinalize.
没有内存高峰。
我在下面尝试了两种方式:
I tried both ways below:
UIImage* tempImage = [UIImage imageWithCGImage:tileImage];
NSData* imageData = UIImageJPEGRepresentation(tempImage, 0.8f); // CRASH HERE.
OR
+ (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。
As I understand it UIImageJPEGRepresentation in turn calls CGImageDestinationFinalize eventually.
编辑:忘了提一下图像是写到硬盘上的,但是大约是一半。
Forgot to mention the images are written to hdd, but about half way through.
这确实确实与IOS 10有关,
This really does seem like something to do with IOS 10, could this be a bug?
我也向苹果提交了该错误,以防万一。
I also filed the bug with apple just in case.
任何帮助或
推荐答案
我将在这里回答自己的问题。
I am going to answer my own question here.
我收集到的内容是,IOS 10无法保存颜色模式为 灰度的文件。
What I have gathered is that IOS 10 has trouble saving a file that has color mode "grayscale".
因此,如果您这样做
UIImageJPEGRepresentation(tempImage, 0.8f)
,如果tempImage为 grayscale ,则会崩溃。
and if tempImage is grayscale you will get a crash.
我正在处理许多图像,直到后来它才与图像的颜色模式相关。
I was processing many images and it didn't click until later that it could be related to the color mode of the image.
我已经向苹果提交了一个错误,以了解他们的意见,但与此同时,我不得不找到一个临时解决方案。
I already filed a bug with apple to see what they say, but meanwhile I had to find a temporary fix.
我的解决方案是通过以下方式将图像转换为RGB:绘制并捕获到新图像中。
My fix was to convert image to RGB by drawing it and capturing it into a new image.
示例方法:
+ (UIImage *)convertImage:(UIImage *)sourceImage
{
UIGraphicsBeginImageContext(sourceImage.size);
[sourceImage drawInRect:CGRectMake( 0, 0, sourceImage.size.width, sourceImage.size.height)];
UIImage *targetImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return targetImage;
}
希望这对某人有所帮助。
Hopefully this helps someone.
这篇关于CGImageDestinationFinalize或UIImageJPEGRepresentation-在IOS 10上保存大文件时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!