CGImageDestinationFinalize或UIImageJPEGRepresentation - 在IOS 10上保存大文件时崩溃

时间:2016-09-26 19:30:10

标签: ios objective-c ios10 core-image

我正在尝试为更大的图像创建切片,而且从IOS 10开始,以下代码似乎不再有效并且与EXC_BAD_ACCESS崩溃。

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

任何大于~1300x1300的图像都会发生崩溃。

在乐器中进行分析并不会产生任何有趣的东西,并指向CGImageDestinationFinalize。

没有记忆飙升。

我尝试了以下两种方式:

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。

编辑:忘记提及图像写入硬盘,但大约一半。

这确实与IOS 10有关,这可能是个错误吗?

为了以防万一,我还提交了苹果错误。

非常感谢任何关于如何写出大图像的帮助或解决方法。

2 个答案:

答案 0 :(得分:4)

我将在这里回答我自己的问题。

我收集到的是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;
}

希望这有助于某人。

答案 1 :(得分:0)

  1. tempimage是您传递给UIImageJPEGRepresentation的参数之一;如果它指向一个死对象,那么当UIImageJPEGRepresentation尝试使用该死对象时,这将导致崩溃。检查tempImage是否为零。
  2. 在目的地
  3. 上致电CGImageDestinationFinalize后,检查您是否要添加图片