【发布时间】:2011-09-06 04:12:43
【问题描述】:
我的应用程序读取从互联网加载的图像并调整其大小;不幸的是,我无法控制这些图像的创建。
最近我发生了一次崩溃,我不确定如何最好地处理。在这种情况下,图像是损坏的 GIF 文件。它没有严重损坏,但它报告的分辨率大小(高度 x 宽度)不准确。该图像应该是 400x600 的图像,但报告的尺寸类似于 1111x999。
sn-p崩溃的代码是:
- (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);
}
}
崩溃日志的sn-p是:
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。
有人对我如何处理或捕获这种情况有任何想法吗?我非常感谢任何建议,因为这让我担心:应用程序稳定性。或者这只是一个直截了当的“向 Apple 报告雷达”?
编辑和补充代码:
所有这些处理都发生在 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 image参数从何而来?你自己创造吗?如何?能发一下损坏的图吗?您是否尝试在您的应用中绘制它? -
@Nikolai Ruhe:感谢 cmets。我已经用更多信息更新了这个问题。我没有尝试绘制图像,因为在这种情况下,场景只是下载、调整大小并存储在磁盘上。
-
通常,在测试 10,000 张图像时,这是我们唯一遇到问题的一张!
-
指定的网址有404。
-
啊,它一定被删除了。我确实有它的副本,但不拥有版权,因此无法分享。
标签: iphone objective-c core-image