Uploading image issue (UIImageJPEGRepresentation vs UIImagePNGRepresentation)

前端 未结 2 2185
旧时难觅i
旧时难觅i 2021-02-08 09:08

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 o

相关标签:
2条回答
  • 2021-02-08 09:29

    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.

    0 讨论(0)
  • 2021-02-08 09:41

    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; 
    
    }
    
    0 讨论(0)
提交回复
热议问题