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

你离开我真会死。 提交于 2019-12-13 17:38:59

问题


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.


回答1:


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

Setting this to NO should solve your problem.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/8216515/why-does-png-format-get-changed-when-on-iphone-or-ipad-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!