Image compression in Xamarin Forms

There are various ways of compressing images in Xamarin forms e.g via Nuget packages such as:

  • Xam.Plugin.Media – setting the compression quality
  • Xamarin.FFimageLoading.Forms – using downsampling feature.

However, if you still require greater compression, then its time to get your hands a little dirty and dive into platform specific implementation (via dependency injection) as unfortunately, image compression is not a feature that is out-of-the box in Xamarin forms.

Firstly, from your Netstardard project, add an IImageCompressionService interface class as shown below:

IImageCompressionService

Thereafter, add the image compression implementation in the  ios project and android project as shown below:

iOS implementationimagecompress_ios

Android implementation

ImageCompression_droid

One thing to note from the above implementation. The compressed image will be stored as a new file. If you do not required the original image, you can simply delete it to optimize on storage.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s