2013-06-12 23 views
0

我在我的iPhone應用程序項目中有一些PNG文件。當我爲模擬器構建時,它們工作正常。但是當我爲該設備構建時,突然每個單獨的png文件都會生成可怕的「當閱讀such-and-such.png pngcrush時發現libpng錯誤:...找不到文件:...」pngcrush抓到libping錯誤,但只有當建設設備

正如我所說,所有東西都可以通過模擬器進行構建和運行。只有當我改變設備的構造時,我纔會得到錯誤。

我試過清洗和重建。

我試着手動刪除Products目錄。

我試過重新啓動我的系統。

我嘗試在不同的項目中使用這些文件(結果相同)。

我發現唯一的作品是打開文件並重新保存它們。但是,這是一個不太理想的解決方案,因爲我有數百個PNG文件都受到這個問題的困擾。我寧願明白問題是什麼,以便我可以直接修復它。

任何想法?

回答

1

這聽起來好像你已經得到了重新壓縮了與蘋果的流氓「pngcrush」 Xcode的計劃,該計劃寫道,無效的PNG文件PNG文件。在「IHDR」應該是的文件開頭附近尋找字符串「CgBI」(從第12個字節開始)。有些應用程序(包括Apple版本的「pngcrush」)可以解決問題。

+0

感謝您的回覆。你是否在說蘋果將一個壞版本的pngcrush與特定版本的XCode捆綁在一起?我從來沒有故意安裝它。 –

+0

他們增加了功能到真正的pngcrush,並使其成爲Xcode SDK的一部分。這發生在幾年前,據我所知現在仍然是2014年的情況。 –

+0

然後,我想我不明白你的最後一句話。你說過「有些應用程序(包括Apple版本的」pngcrush「)可以解決問題。」但是你不是說蘋果版本的pngcrush是造成這個問題的原因嗎? –

0

通過編寫一個快速而又髒的遞歸文件重新保護來解決此問題。我已經證實,僅僅對我的項目目錄運行這個修復了我看到的459錯誤。這是相關的代碼,以幫助任何人。

- (IBAction) btnGo_Pressed:(id) sender { 
    // The path to search is specified by the user 
    NSString *path = self.txtPathToSearch.stringValue; 

    // Recursively find all files within it 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *subpaths = [fileManager subpathsOfDirectoryAtPath:path error:nil]; 

    // Look for pngs 
    int totalImagesResaved = 0; 
    for (int j=0; j<[subpaths count]; j++) { 

     NSString *fullPath = [path stringByAppendingPathComponent:[subpaths objectAtIndex:j]]; 

     // See if this path ends with a ".png" 
     if ([fullPath compare:@".png" options:NSCaseInsensitiveSearch range:NSMakeRange([fullPath length] - 4, 4)] == NSOrderedSame) { 

      // Got one. Now resave it as a png 
      NSImage *image = [[NSImage alloc] initWithContentsOfFile:fullPath]; 
      [self saveImage:image asPngWithPath:fullPath]; 
      totalImagesResaved++; 
     } 
    } 

    // Status report 
    NSAlert *alert = [NSAlert alertWithMessageText:@"Done" defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@"Encountened %li paths. Resaved %i .pngs.", (unsigned long)[subpaths count], totalImagesResaved]; 
    [alert runModal]; 
} 

- (void) saveImage:(NSImage *) image asPngWithPath:(NSString *) path 
{ 
    // Cache the reduced image 
    NSData *imageData = [image TIFFRepresentation]; 
    NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; 
    imageData = [imageRep representationUsingType:NSPNGFileType properties:nil]; 
    [imageData writeToFile:path atomically:YES]; 
} 
相關問題