Frage

I have some png files in my iPhone app project. They work fine when I build for the simulator. But when I build for the device, suddenly every single png file generates the dreaded "while reading such-and-such.png pngcrush caught libpng error: ... Could not find file: ..."

As I say, everything builds and runs great with the simulator. It's only when I change the scheme to build for the device that I get the errors.

I tried cleaning and rebuilding.

I tried manually deleting the Products directory.

I tried restarting my system.

I tried using the files in a different project (same results there).

The only thing I have found that works is to open the files and resave them. However, this is a less than optimal solution because I have hundreds of PNG files all suffering from this issue. I would rather understand what the issue is so that I can fix it directly.

Any ideas?

War es hilfreich?

Lösung

It sounds as though you've got PNG files that were recompressed with Apple's rogue "pngcrush" Xcode program, which writes files that are not valid PNGs. Look for the string "CgBI" near the beginning of the file (starting at the 12th byte) where "IHDR" should be. There are applications (including the Apple version of "pngcrush") that can undo the problem.

Andere Tipps

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];
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top