Uploading image issue (UIImageJPEGRepresentation v

2019-03-18 23:51发布

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?

2条回答
何必那么认真
2楼-- · 2019-03-19 00:18

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; 

}
查看更多
Evening l夕情丶
3楼-- · 2019-03-19 00:28

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.

查看更多
登录 后发表回答