Converting UIImage to Byte Array

前端 未结 3 1089
南笙
南笙 2020-12-03 03:48

I need to convert a UIImage to a byte array. I am using Xamarin\'s Visual Studio plugin to produce an iOS application.

The bit of code below gets the image, but I

相关标签:
3条回答
  • 2020-12-03 04:01

    It's been a long time since I checked the ServiceStack API but, if possible, try to avoid byte[] since it will need to create another copy of the same data. It does not matter in many cases but images are often huge.

    E.g. if there's an overload that accept a System.IO.Stream then use image.AsPNG ().AsStream () and avoid the overhead :-)

    0 讨论(0)
  • 2020-12-03 04:13

    Shamelessly stolen from ChrisNTR

    using (NSData imageData = image.AsPNG()) {
      Byte[] myByteArray = new Byte[imageData.Length];
      System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length));
    }
    
    0 讨论(0)
  • 2020-12-03 04:13
    Stream pst =   img.AsPNG().AsStream();
    
    0 讨论(0)
提交回复
热议问题