0

我的 iPhone 应用程序项目中有一些 png 文件。当我为模拟器构建时,它们工作正常。但是当我为设备构建时,突然每个单独的 png 文件都会生成可怕的“在读取 such-and-such.png pngcrush 时捕获 libpng 错误:...找不到文件:...”

正如我所说,一切都在模拟器上构建和运行得很好。只有当我更改为设备构建的方案时,才会出现错误。

我尝试清洁和重建。

我尝试手动删除 Products 目录。

我尝试重新启动我的系统。

我尝试在不同的项目中使用这些文件(那里的结果相同)。

我发现唯一可行的方法是打开文件并重新保存它们。但是,这不是最佳解决方案,因为我有数百个 PNG 文件都遇到了这个问题。我宁愿了解问题是什么,以便我可以直接解决它。

有任何想法吗?

4

2 回答 2

1

听起来好像您有使用 Apple 的流氓“pngcrush”Xcode 程序重新压缩的 PNG 文件,该程序写入的文件不是有效的 PNG。查找文件开头附近的字符串“CgBI”(从第 12 个字节开始),其中“IHDR”应该是。有一些应用程序(包括 Apple 版本的“pngcrush”)可以解决这个问题。

于 2014-02-10T19:19:54.060 回答
0

Worked around this issue by writing a quick-and-dirty recursive file re-saver. I've verified that simply running this against my project directory fixes the 459 errors I was seeing. Here's the pertinent code in case it helps anyone.

- (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];
}
于 2013-06-13T17:18:27.027 回答