views:

238

answers:

2

Hi,

I've inherited a project that at some point creates a zip file, adds an XML file to the zip and then adds a number of PNG files to the same archive. All works fine on the simulator, but whenever I run the same code on the device itself the resulting png files are altered and unopenable when opened on my Mac.

They still appear to be png files, but the 'corrupt' export ones are slightly larger than the true files are, and hex dumps show the contents differ drastically. Headers are preserved though...

Original:

00000000  89 50 4e 47 0d 0a 1a 0a  00 00 00 0d 49 48 44 52  |.PNG........IHDR|
00000010  00 00 00 6d 00 00 00 75  08 06 00 00 00 44 7d 6f  |...m...u.....D}o|
00000020  a0 00 00 00 19 74 45 58  74 53 6f 66 74 77 61 72  |?....tEXtSoftwar|
00000030  65 00 41 64 6f 62 65 20  49 6d 61 67 65 52 65 61  |e.Adobe ImageRea|
00000040  64 79 71 c9 65 3c 00 00  38 32 49 44 41 54 78 da  |dyq?e<..82IDATx?|

Corrupt:

00000000  89 50 4e 47 0d 0a 1a 0a  00 00 00 04 43 67 42 49  |.PNG........CgBI|
00000010  30 00 20 02 10 f3 44 7c  00 00 00 0d 49 48 44 52  |0. ..?D|....IHDR|
00000020  00 00 00 6d 00 00 00 75  08 06 00 00 00 44 7d 6f  |...m...u.....D}o|
00000030  a0 00 00 00 19 74 45 58  74 53 6f 66 74 77 61 72  |?....tEXtSoftwar|
00000040  65 00 41 64 6f 62 65 20  49 6d 61 67 65 52 65 61  |e.Adobe ImageRea|
00000050  64 79 71 c9 65 3c 00 00  38 65 49 44 41 54 ed bd  |dyq?e<..8eIDAT??|

(I appreciate a small chunk of the file header is not very helpful but the intention is to show that the corruption occurs within the PNG and not the ZIP itself.)

So I guess what I'm asking is whether anyone has experienced anything like this before? I just tried using the following wrapper http://www.flyblog.info/catprogramming/202.html and experienced the same issue, so am guessing its libzip itself causing the issue?

Does anyone have a simple, tried and tested method for adding files to a zip file on the Ipod that I can try and swap in?

If it helps, here is the code that creates the zip:

ZipArchive* zip = [[ZipArchive alloc] init];

BOOL ret = [zip CreateZipFile2: zipPath];
NSMutableSet *imageNames = [NSMutableSet set];
[curAlbum collectImageNames:imageNames];

for (NSString *imageName in imageNames) {
 NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@""];
 NSLog(imagePath);
 ret = [zip addFileToZip:imagePath newname:@"test.png"];
}



[zip release];

Any advice appreciated :-)

+1  A: 

When Xcode builds an application for the device, it alters any PNG resources, converting them to BGRA (as opposed to the usual RGBA) and premultiplying the alpha channel. There isn't a way to prevent this using any project settings that I know of; you might try giving the resources you include with the app an extension other than ".png" to see if Xcode then copies them without alteration.

Noah Witherspoon
+2  A: 

In addition to Noah's answer Xcode also compresses PNG files:

Xcode Build Settings

It might be worth setting COMPRESS_PNG_FILES = NO in the target info window.

rjstelling
My word that was a nightmare - this option solved it though so happy days :-) Thanks a lot (I wasn't sure whose answer to accept as the right one but following these instructions got the app sorted so went with it - thanks to Noah for the swift reply also)
davbryn