gpt4 book ai didi

objective-c - 是什么导致我的 iPad 应用程序出现此 EXC_CRASH?

转载 作者:行者123 更新时间:2023-11-29 13:50:24 24 4
gpt4 key购买 nike

我从我的 iPad 应用程序的崩溃报告中得到了这个符号化的堆栈跟踪(摘录):

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x00000000, 0x00000000
Crashed Thread: 0

0 ImageIO 0x34528eb4 _CGImagePluginIdentifyPNG + 0
1 ImageIO 0x34528d90 _CGImageSourceBindToPlugin + 368
2 ImageIO 0x34528bda CGImageSourceGetCount + 26
3 UIKit 0x341b8f66 _UIImageRefAtPath + 366
4 UIKit 0x342650ce -[UIImage initWithContentsOfFile:] + 50
5 UIKit 0x342b0314 +[UIImage imageWithContentsOfFile:] + 28
6 DesignScene 0x00013a2a -[LTImageCache fetchImageforURL:] (LTImageCache.m:37)

-[LTImageCache fetchImageforURL:] 的内容如下:

- (UIImage *)fetchImageforURL:(NSString *)theUrl {
NSString *key = theUrl.md5Hash;
return [UIImage imageWithContentsOfFile:[self filenameForKey:key]];
}

-[LTImageCache filenameForKey:]的内容:

- (NSString *) filenameForKey:(NSString *) key {
return [_cacheDir stringByAppendingPathComponent:key];
}

_cacheDir ivar 是在 -init 中创建和保留的。那么问题来了,是什么导致了这次崩溃?问题是:

  1. 需要保留-[LTImageCache filenameForKey:]的返回值(自动释放)
  2. 某处未处理的异常(+[UIImage imageWithContentsOfFile:] 声称如果图像无法识别则返回 nil)
  3. 别的……我猜不到

我认为自动释放的值会很好。事实上,这段代码几个月来一直运行良好,这个方法在一个 session 中被调用了 100 次。这是在非常特殊的情况下发生的罕见崩溃(应用程序在一夜之间加载,早上解锁 iPad 时崩溃)。

是什么导致了这次崩溃?

最佳答案

我猜是的,但它看起来像一个伪造的图像文件。这是在您的应用程序包中还是您下载了它?

我认为这与内存管理没有任何关系。

要进行测试,您可以尝试使用 ImageIO 自己打开文件。

  CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)self.url, NULL);
if(NULL != imageSource) {
size_t imageCount = CGImageSourceGetCount(imageSource);
if(imageCount > 0) {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], kCGImageSourceCreateThumbnailFromImageIfAbsent,
[NSNumber numberWithInteger:maxSize], kCGImageSourceThumbnailMaxPixelSize, nil];
CGImageRef thumbImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (CFDictionaryRef)options);
self.image = [UIImage imageWithCGImage:thumbImage scale:scale orientation:imageOrientation];
CGImageRelease(thumbImage);
CFRelease(imageSource);
[pool drain];
}
} else {
NSLog(@"Unable to open image %@", self.url);
}

然后尝试查找图像数。

使用 maxSize 并获取缩略图将确保您不会加载 5 兆像素的图像以放入 UI 上的 100x100 磁贴中。

scale 是窗口的比例(iPhone 4 为 2,其他为 1)。

要找到方向,您需要使用 CGImageSourceCopyPropertiesAtIndex 函数,然后使用 kCGImagePropertyOrientation 键来获取特定图像的方向。

关于objective-c - 是什么导致我的 iPad 应用程序出现此 EXC_CRASH?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5034482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com