Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

MediaFile returns null on iOS

LarsNymandLarsNymand DKMember ✭✭

I am trying to fix 90 degrees rotation on iOS using the GetStreamWithImageRotatedForExternalStorage() method instead of GetStream().

The issue I have is that GetStream() works but return the image rotated, but when using GetStreamWithImageRotatedForExternalStorage() it returns an empty stream?

First I get the file using:

file = await CrossMedia.Current.PickPhotoAsync()

Then that file is passed to below method:

public static byte[] GetCropedImage(MediaFile file)
{
  using (var inputStream = new SKManagedStream(file.GetStreamWithImageRotatedForExternalStorage()))
  {
    using (var original = SKBitmap.Decode(inputStream))
    {
Here original is null?
    }
  }
}

I use the latest version of MediaPlugin 4.0.1.1 and SkiaSharp 1.60.2.

Both GetStream() and GetStreamWithImageRotatedForExternalStorage() works just fine on Android both with the image rotated correctly.

Anyone having an idea what I am doing wrong or are we talking about a bug?

Best Answer

  • LarsNymandLarsNymand DKMember ✭✭
    Accepted Answer

    Hi @ColeX thank you for the links. I already looked at those but didn't find a working solution. or at least not a "simple" one.

    What I ended up doing was using .GetStream() and then rotate the image in case of iOS like below:

    using (var original = SKBitmap.Decode(inputStream))
    {
      //iOS rotation bug fix
      if (Device.RuntimePlatform == Device.iOS)
      {
        using (var rotated = new SKBitmap(original.Height, original.Width))
        {
          using (var surface = new SKCanvas(rotated))
          {
            surface.Translate(rotated.Width, 0);
            surface.RotateDegrees(90);
            surface.DrawBitmap(original, 0, 0);
          }
          rotated.CopyTo(original);
        }
      }
      .....
    

    Also I am still using the default settings for

    file = await CrossMedia.Current.PickPhotoAsync()
    

    Thank you anyway!

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
  • LarsNymandLarsNymand DKMember ✭✭
    Accepted Answer

    Hi @ColeX thank you for the links. I already looked at those but didn't find a working solution. or at least not a "simple" one.

    What I ended up doing was using .GetStream() and then rotate the image in case of iOS like below:

    using (var original = SKBitmap.Decode(inputStream))
    {
      //iOS rotation bug fix
      if (Device.RuntimePlatform == Device.iOS)
      {
        using (var rotated = new SKBitmap(original.Height, original.Width))
        {
          using (var surface = new SKCanvas(rotated))
          {
            surface.Translate(rotated.Width, 0);
            surface.RotateDegrees(90);
            surface.DrawBitmap(original, 0, 0);
          }
          rotated.CopyTo(original);
        }
      }
      .....
    

    Also I am still using the default settings for

    file = await CrossMedia.Current.PickPhotoAsync()
    

    Thank you anyway!

Sign In or Register to comment.