Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading image issue (UIImageJPEGRepresentation vs UIImagePNGRepresentation)

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?

like image 546
Mustafa Avatar asked Jan 30 '09 10:01

Mustafa


2 Answers

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.

like image 134
andynormancx Avatar answered Oct 23 '22 12:10

andynormancx


Regarding your third point, you can determine the image type loaded from the library in your image picker delegate method as follows:

- (void)imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info {

    // Get the image reference URL
    NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];

    // The path extension contains the image type: JPG, PNG, GIF, etc.
    NSString *originalImageType = referenceURL.pathExtension; 

}
like image 35
nurider Avatar answered Oct 23 '22 10:10

nurider