Uploading image issue (UIImageJPEGRepresentation vs UIImagePNGRepresentation)

半城伤御伤魂 提交于 2019-12-03 17:04:34

问题


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?


回答1:


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.




回答2:


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; 

}


来源:https://stackoverflow.com/questions/495096/uploading-image-issue-uiimagejpegrepresentation-vs-uiimagepngrepresentation

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