我的应用程序读取并调整从互联网加载的图像;不幸的是我无法控制这些图像的创作。关于如何在CGImageDestinationFinalize中处理这个崩溃的任何建议?
最近我有一个崩溃,我不知道如何能够最好地处理。在这种情况下,图像是一个损坏的GIF文件。它没有被严重损坏,但它报告的分辨率大小(高x宽)并不准确。图片应该是400x600的图片,但报道的内容类似1111x999。
崩溃的代码片段:
- (void) saveRawImageRefToDisk:(CGImageRef)image withUUID:(NSString *)uuid andType:(NSString *)type {
if (!uuid || !type) {
return;
}
NSDictionary *imageDestinationWriteOptions = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1], (id)kCGImagePropertyOrientation,
(id)kCFBooleanFalse, (id)kCGImagePropertyHasAlpha,
[NSNumber numberWithInt:1.0], kCGImageDestinationLossyCompressionQuality,
nil];
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:[self getRawImagePathForUUID:uuid]], (CFStringRef)type, 1, nil);
if (imageDestination) {
CGImageDestinationAddImage(imageDestination, image, (CFDictionaryRef)imageDestinationWriteOptions);
CGImageDestinationFinalize(imageDestination); //<--- EXC_BAD_ACCESS in here
CFRelease(imageDestination);
}
}
崩溃日志的片段是:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x06980000
Crashed Thread: 8
Thread 8 name: Dispatch queue: com.apple.root.default-priority
Thread 8 Crashed:
0 libsystem_c.dylib 0x348cf6a4 memcpy$VARIANT$CortexA9 + 212
1 CoreGraphics 0x366de30c CGAccessSessionGetBytes + 112
2 ImageIO 0x32941b04 GenerateFromRGBImageWu + 256
3 ImageIO 0x329432aa _CGImagePluginWriteGIF + 3390
4 ImageIO 0x32932b3a CGImageDestinationFinalize + 118
有问题的图像是一个GIF,所以我不得不在我的方法类型设置为@“com.compuserve.gif”。据我可以告诉CGImageDestinationRef imageDestination是一个有效的对象。
我的应用程序的部分逻辑是保持图像的格式与阅读时相同。所以在这种情况下,我正在调整大小并写回GIF。
我试着写出CGImageRef作为PNG,有趣的是应用程序没有崩溃。它写出了PNG(这是无效的 - 只是一个空白的黑色图像),但它没有崩溃。但是在正常的执行过程中,我不知道我应该写一个GIF作为PNG。
任何人有任何想法,我如何处理或陷入这种情况?我会大大appreaciate任何建议,因为这是我的一个担心:应用程序的稳定性。或者这只是一个直接的“作为苹果雷达报告”?
编辑&补充代码:
所有该处理的NSOperationQueue内,而不是在主线程发生
图片中的问题是提供在http://dribbble.com/system/users/1409/screenshots/182540/htg-logo-tiny.gif?1306878218
下面是函数我在哪里创建CGImageRef:
- (CGImageRef) createIpadSizedImageRefFromImageSource:(CGImageSourceRef)imageSource withSourceSizeOf(CGSize)imageSourceSize {
NSDictionary *imageCreationOptions = [NSDictionary dictionaryWithObjectsAndKeys:
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
[NSNumber numberWithInt:1024], kCGImageSourceThumbnailMaxPixelSize,
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageAlways,
nil];
CGImageRef image = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (CFDictionaryRef)imageCreationOptions);
if (!image) {
return nil;
} else {
[(id) image autorelease];
return image;
}
}
和CGImageSourceRef从文件路径创建这样的:
- (CGImageSourceRef) createImageSourceFromURL:(NSURL *)imageLocationOnDisk {
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageLocationOnDisk, NULL);
if (!imageSource) {
DebugLog(@"Issue creating image source for image at: %@", imageLocationOnDisk);
return nil;
} else {
[(id) imageSource autorelease];
return imageSource;
}
}
“CGImageRef图像”参数来自哪里?你自己创造它吗?怎么样?你能发布损坏的图像吗?你有没有尝试在你的应用中绘制它? – 2011-06-01 10:31:15
@Nikolai Ruhe:感谢您的评论。我更新了更多信息的问题。我没有尝试绘制图像,因为在这种情况下,场景仅仅是下载,调整大小和存储在磁盘上。 – Damien 2011-06-01 10:43:10
通常,在测试10,000张图像时,这是我们遇到的唯一问题! – Damien 2011-06-01 10:45:52