maui icon indicating copy to clipboard operation
maui copied to clipboard

Excessive Increase in Image File Size Upon Saving in iOS platform

Open devanathan-vaithiyanathan opened this issue 1 year ago • 2 comments

Description

When saving an image in iOS platform, we've observed a significant increase in the file size compared to the original image. For instance, an image with an initial size of 300KB is being saved with a size of 800KB or more.

Steps to Reproduce

1.Create a new MAUI application. 2.Add Image and button control,

MainPage.xaml

> <Grid RowDefinitions="*, auto">
>     <Image x:Name="image" Source="desktop.jpg"/>
>     <Button Text="Save" WidthRequest="150" Clicked="OnSaveButtonClicked" Grid.Row="1"/>
> </Grid>

3.Add this code in click event,

MainPage.xaml.cs

>         private void OnSaveButtonClicked(object sender, EventArgs e)
>         {
> #if IOS
>             if (this.image != null && this.image.Handler != null && this.image.Handler.PlatformView is UIImageView uiImage)
>             {
>                 var nativeImage = uiImage.Image;
>                 if (nativeImage == null)
>                 {
>                     return;
>                 }
> 
>                 var formattedImage = nativeImage.AsJPEG();
>                 if (formattedImage == null)
>                 {
>                     return;
>                 }
> 
>                 Stream? stream = formattedImage.AsStream();
>                 if (stream == null || stream == Stream.Null)
>                 {
>                     return;
>                 }
> 
>                 string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
> 
>                 NSData? imageData = NSData.FromStream(stream);
>                 if (imageData != null)
>                 {
>                     imageData.Save(filePath, false, out NSError? nsError);
> 
>                 }
>             }
> #endif
>         }

Link to public reproduction project repository

https://github.com/devanathan-vaithiyanathan/iOSImageSaveIssue

Version with bug

Unknown/Other

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

iOS, macOS

Affected platform versions

Did you find any workaround?

No response

Relevant log output

No response

I don't believe this is a MAUI UI issue, all of those APIs are UIKit. What you're doing is

  • Getting the UIKit UIImageView from the MAUI Image
  • Converting it to a JPEG (You're taking the loaded UIImageView image and converting that to a JPEG)
  • Saving it to disk

Which isn't related to MAUI, that's standard UIKit APIs. You can control the compression of AsJPEG through an NFloat parameter, as you can with AsPNG.

drasticactions avatar Feb 20 '24 11:02 drasticactions

Hi @devanathan-vaithiyanathan. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Feb 20 '24 19:02 ghost

Don't think we need more info here. As pointed out, this is how that API works on iOS. If you want to add compression or something of sorts you'll have to do that in your code. This is not something that .NET MAUI provides for.

jfversluis avatar Feb 21 '24 10:02 jfversluis