0

I am working in xamarin.ios. I am capturing a picture with help of camera and trying to upload it on server. But when I am trying to upload it on server I am getting "TargetInvocationException". But when I am running same code on Ipad everything is working fine.

Following is the code :

  Camera.TakePicture(this, (obj) =>
            {
                var photo = obj.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage;
                Byte[] myByteArray;
                using (NSData imageData = photo.AsJPEG(0.0f))
                {
                    //myByteArray = imageData.ToArray();
                    myByteArray = new Byte[imageData.Length];
                    System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, myByteArray, 0, Convert.ToInt32(imageData.Length));
                }

                ImageLoaderPopup imageLoader = new ImageLoaderPopup(this, selectedWorkOrder, myByteArray, title);
                imageLoader.PopUp(true, delegate { });
            });

Does anyone know why I am facing this problem? What am I doing wrong?

anand
  • 1,399
  • 5
  • 23
  • 55

1 Answers1

0

You might be having problem with the size of the image returned by the Picker. Images taken with the iPhone 7 are huge in size.

You just need to scale down the original image before uploading it using the Scale method and setting the size you find acceptable.

var smallImage = image.Scale(new CGSize(1280, 720)); //Or the size you need.

UPDATE

All you need is the Scale method mentioned above.

The big advantage of Xamarin being Open Source is that you can look at the internals any time you have a doubt.

https://github.com/xamarin/xamarin-macios/blob/master/src/UIKit/UIImage.cs#L66

pinedax
  • 9,246
  • 2
  • 23
  • 30