Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Png image is being converted to jpg when selected from gallery in case of iOS #264

Closed
HabibAli opened this issue Jun 2, 2017 · 4 comments

Comments

@HabibAli
Copy link

HabibAli commented Jun 2, 2017

If you are creating an issue for a BUG please fill out this information. If you are asking a question or requesting a feature you can delete the sections below.

Failure to fill out this information will result in this issue being closed. If you post a full stack trace in a bug it will be closed, please post it to http://gist.github.com and then post the link here.

Bug Information

Version Number of Plugin: 2.6.2
Device Tested On: Any iOS Device
Simulator Tested On: Any iOS Simulator
Version of VS: Testing it on Mac Mini
Version of Xamarin: 2.3.3.193
Versions of other things you are using: -

Steps to reproduce the Behavior

Just select a png photo from gallery using following code snippet

Expected Behavior

Image should be png and file path should end with .png

Actual Behavior

Image is jpg and filepath is ending with .jpg

Code snippet

if (CrossMedia.Current.IsPickPhotoSupported)
                        {
                            string filePath = null;

                            using (MediaFile file = await CrossMedia.Current.PickPhotoAsync())
                            {
                                InsightsFacade.Track("ImageReceivedFromGallery");

                                if (file != null)
                                {
                                    filePath = file.Path;

                                    if (filePath != null)
                                        await this.OnPhotoReceived(filePath);
                                }
                            }
                        }

Screenshotst

images

@Nerololo
Copy link

I think iOS store images in gallery as .jpg by default, try to take a photo with the camera an you will see is a .jpg

@jamesmontemagno
Copy link
Owner

Everything is stored as jpg by design

@jgold6
Copy link
Contributor

jgold6 commented Oct 7, 2019

@jamesmontemagno
I suggest you re-open this. The gallery can store PNG files and you can get them back as PNG files.

I just had a case where a customer added a PNG file to the gallery and when getting it back it lost transparency because it was returned as a jpg. However I was able to have them workaround this issue by creating their own dependency service, checking the path of the image returned from the iOS UIImagePickerController and if it is PNG, then you can create a stream as a PNG. So it is possible to store and retrieve PNG images in the iOS photo gallery. The lines where you are forcing the image to be a JPG are similar to this line:
https://github.com/jamesmontemagno/MediaPlugin/blob/master/src/Media.Plugin/iOS/MediaPickerDelegate.cs#L477

Our guide for a Forms pick photo dependency service does the same thing:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/photo-picker#ios-implementation

See this code:

void OnImagePickerFinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs args)
    {
        UIImage image = args.EditedImage ?? args.OriginalImage;

        if (image != null)
        {
            // Convert UIImage to .NET Stream object
            NSData data = image.AsJPEG(1);
            Stream stream = data.AsStream();
            ...
    }

The above forces the image to be returned to the caller as a JPG, but if you do this:

void OnImagePickerFinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs args)
    {
        UIImage image = args.EditedImage ?? args.OriginalImage;

        if (image != null)
        {
            // Convert UIImage to .NET Stream object
            NSData data;
            if (args.ReferenceUrl.PathExtension == "PNG" || args.ReferenceUrl.PathExtension == "png")
                data = image.AsPNG();
            else
                data = image.AsJPEG(1);
            Stream stream = data.AsStream();
            ...
    }

Then you get a PNG back. There would seem to be no reason why you can't do similar in this library.

I will work on a pull request.

jgold6 added a commit to jgold6/MediaPlugin that referenced this issue Oct 8, 2019
Fixes issue jamesmontemagno#264 Png image is being converted to jpg when selected from
gallery in case of iOS
- Checks for the file extension when first getting the photo info after
user picks it
- Sets a Static variable, photoType, with “png” or “jpg” depending on
photo type retrieved from Photos app.
- Uses that static variable, photoType, to determine the photo type to
save and return.
- This preserves any png transparency that may have been in the PNG
file saved in the photos app.
@jgold6 jgold6 mentioned this issue Oct 8, 2019
@jgold6
Copy link
Contributor

jgold6 commented Oct 8, 2019

@Nerololo

Yes, if you take a picture, it will store as a JPEG, but there are other ways to get images into the Photos app, like saving a PNG image from a website for one.

I just did a pull request that should fix this issue. It checks the file extension of the selected image, and if a PNG saves it and passes it back as a PNG instead of a JPG.

jamesmontemagno added a commit that referenced this issue Oct 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants