2011-02-17 142 views
1

我從崩潰報告中symbolicated堆棧跟蹤從我的iPad應用程序(節選):在我的iPad應用程序中導致此EXC_CRASH的原因是什麼?

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]; 
} 

ivar創建並保留在-init。所以問題是,造成這次事故的原因是什麼?是,這個問題:

  1. -[LTImageCache filenameForKey:]返回值需要保留(它的自動釋放)
  2. 未處理的異常某處(+[UIImage imageWithContentsOfFile:]要求返回nil如果圖像是無法識別)
  3. 別的東西......我'猜出來了

我會認爲autoreleased的價值會很好。實際上,這段代碼幾個月來一直工作正常,而且這種方法在會話中被稱爲100次。在非常特殊的情況下,這是一次罕見的崩潰(該應用程序在一夜之間被加載,早上解鎖iPad時發生崩潰)。

這是什麼原因造成的?

回答

1

我猜,但它看起來像一個假圖像文件。這是在您的應用程序包中,還是您下載它?

我不認爲它與內存管理有任何關係。

要測試您可以嘗試使用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百萬像素的圖像,以便將其放入用戶界面上的100x100圖塊中。

scale是窗口的比例(對於iPhone 4和其他任何其他應用,將爲2)。

要找到方向,您需要使用CGImageSourceCopyPropertiesAtIndex函數,然後使用kCGImagePropertyOrientation鍵來獲取特定圖像的方向。

+0

該文件已下載。在iOS中是`CGImageSource`嗎?該文檔僅提及Mac OS X 10.4或更高版本。無論如何,這隻發生在一個非常特殊的情況下(當應用程序被打開,但iPad被鎖定在一夜之間)。所以我想知道是否應該嘗試捕獲異常並刪除文件。這看起來合理嗎? – theory 2011-02-22 04:21:42

相關問題