Davy R
Davy R

Reputation: 39

Why does png format get changed when on iPhone or iPad Device

Interesting one this. I did do a bit Googling on the it but here we go. I have this png file. What I want to do is have it on my iPhone and send it to a server application on Windows. I use something like this:

NSString *filePath = [[NSBundle mainBundle] pathForResource:IconFile ofType:@"png"]; 

NSData *icon = [[NSData alloc ] initWithContentsOfFile:filePath];             
NSLog(@"File path is %@",filePath);
NSLog(@" Bytes to send in Icon File %d",icon.length);

Now this works just fine when I am on the iPhone simulator. When I go to the device though the png format grows in size. For example I had one that was 2514 bytes and went up to 2652 bytes. When I transmit this file - its not able to be read by the Windows app.

So I assume that when a png file gets copied over in the resource bundle - it must get optimised or something. I can get round it by changing the extension to say .txt - then the file doesnt change.

Does anyone know why that is ? And can you prevent it being changed as I'd rather keep the correct extension. I have seen that png formats need to be converted when you get them from the iPhone but I dont know why this would happen when you upload one and it doesn't work in the simulator mode. Happens with both iPhone and iPad at iOS 5.

Upvotes: 1

Views: 2541

Answers (4)

jrturton
jrturton

Reputation: 119242

The modifying of the png images can be prevented in the build settings - under Packaging there is an option to compress PNG files:

enter image description here

Setting this to NO should solve your problem.

Upvotes: 4

Till
Till

Reputation: 27597

What happens is documented within this document: Viewing iPhone-Optimized PNGs

In short, Apple is using their patched version of pngcrush (note, this one differs from the one available through sourceforge).Their optimizing includes premultiplying the alpha values to speed up the loading process.

You may, as described by the linked document, revert this optimizing if needed (e.g. when reversing existing apps).

The easiest way to prevent this optimizing is to change/remove the file-extension when adding those images to your project.

Upvotes: 1

Mundi
Mundi

Reputation: 80265

Yes, Apple optimizes all PNGs for display on the iPhone. For example, if you go to iOS .app in the Finder and view its "contents", you will see that all PNGs do not display correctly on Mac OS X.

The solution is to import your file as a "file" rather than an "image". One easy way to do this is to remove the extension then drag it into your project. Another way is to select the image in Xcode and change the File Type under the Identity and Type section in the assistant editor.

Upvotes: 0

Accatyyc
Accatyyc

Reputation: 5828

PNG's are converted to a format "optimized for iPhone". Now this is just what I've read and I'm unsure what the optimization is and if it's actually an optimization, but that's what Apple has chosen to do. Maybe the iPhone GPU is quicker in loading their own format because of hardware support.

In any case, this happens to all PNG images when you deploy a project, and there is no way to prevent it that I know.

If your app downloads a PNG from a remote server this optimization will not occur and you can freely work with the file.

Upvotes: 0

Related Questions