views:

3322

answers:

1

I have stored a few pictures using Safari's "Hold + Save Image" option to my Photo Library in simulator. When I pick an image from the library, I have to convert it to JPEG or PNG so I can upload it to the server. Issue is that the size of original image is around 200 KB but the image after converting to PNG is around 2 MB. I'm using UIImagePNGRepresentation to convert UIImage object to NSData and then posting the image.

  1. What am I missing here? Why is the size of picture getting larger than its original size? How can I prevent this?

  2. What's the difference between UIImageJPEGRepresentation and UIImagePNGRepresentation? Which one should be used (recommended)?

  3. Can I determine the type of image loaded from photo library?

  4. What is the default type of images captured by the iPhone camera (might seem a silly question)?

+3  A: 

I'm guessing that the original image is stored as a JPEG.

PNG is designed for storing things like screen shots and line drawing. It is not designed for storing things like photos.

It all comes down to the type of compression used, PNG uses lossless compression so that the image will be exactly the same as the original image. JPEG uses lossy compression, the resulting image is an approximation to the original.

If you take a lossy JPEG and then save it as a PNG then it will increase in size, often by a large amount as you have seen.

The solution to your problem is to do nothing to your images before you upload them. They will already be PNG, GIF or JPEG images. That is what you should upload.

The format of the images saved by the iPhone camera is JPEG.

Sound like you probably need to do some reading up on PNG and JPEG generally.

andynormancx
Thanks for your response. I'm using POST method to upload my images and for that i don't know any other way except to convert the UIImage to NSData through UIImageJPEGRepresentation or UIImagePNGRepresentation methods (providing "Content-Type: image/jpeg" in post request).
Mustafa
How can i achieve the result of uploading the image with its original format?
Mustafa
I'm not clued up on iPhone development I'm afraid. But I'd imagine that you just need to load the file in, convert it to NSData and upload that.I'd be surprised if you even needed to touch the JPEG/PNG classes just to upload the data in the file. Sorry I can't be any more help.
andynormancx
I quick bit of Googling suggests I am probably wrong about you not needing UIImageJPEGRepresentation http://forums.macrumors.com/showthread.php?t=558573
andynormancx
Thanks for sharing the information.
Mustafa