1

I have a QR code image in the format xamarin.forms.image created using zxing. My requirement is to show this QR code on apple watch. this image is saved to a global variable like below

stackQRCode.Children.Add(zXingBarcodeImageView);
 App.QRCodeImage = zXingBarcodeImageView

I am trying with wcsessionmanager.But in the UpdateApplicationContext, the image is not getting sent. anyone with solution? What is the simplest way to achieve this?Thanks in Advance.

iOS Developer
  • 1,723
  • 2
  • 16
  • 47

1 Answers1

1

The Easiest way to convert a Xamarin Forms Image into a UIImage and vice versa would be byte[] conversion:

UIImage to Byte[]:

byte[] byteArray;

using (NSData imageData = originalImage.AsJPEG())
{
  byteArray = new Byte[imageData.Length];
  System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, byteArray, 0, Convert.ToInt32(imageData.Length));
}

Byte[] to Xamarin.Forms.ImageSource:

var XamImageSource=ImageSource.FromStream(() => new MemoryStream
            (byteArray));

Now the catch is the other way around Xamarin.Forms Image/ImageSource do not expose any API's to directly get the image stream or byte[] from it.(In my Knowledge)

So you can use FFImageLoading instead!!

Which has an API to get the RawImage as follows

FFImageLaoding to byte[]:

var bytes = await ImageView.GetImageAsJpgAsync(); //png method also available

byte[] to UIImage

 var data = NSData.FromArray(byteData);
 var uiimage = UIImage.LoadFromData(data);
FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • originalImage is of xamarin.forms.image and AsJPEG is not available. The error getting is 'image doest not contain defiition for AsJPEG and no accessible extension'. – iOS Developer Feb 13 '19 at 07:48
  • Please check the part of my answer where i said the following `Now the catch is the other way around Xamarin.Forms Image/ImageSource do not expose any API's to directly get the image stream or byte[] from it.(In my Knowledge)` – FreakyAli Feb 13 '19 at 07:54
  • What are you using to come up with the xamarin forms image? do you have a weburl or something like that of the source image? – FreakyAli Feb 13 '19 at 09:12
  • Can you add the relevant bit to the question that would help – FreakyAli Feb 13 '19 at 09:20
  • What does the barcodeWriter's write method return to you does it return a byteArray or a UIImage? – FreakyAli Feb 13 '19 at 10:54