2

In my Xamarin.iOS application, I have a byte array that I need to convert to a UIImage. I tried this:

var data = NSData.FromArray(myByteArray);
var uiimage = UIImage.LoadFromData(data);

But it won't work. It creates the UIImage, but when I set it to a UIImageView I don't see anything. I used the same byte array on the Android platform of my project and it works fine. So I assume the way I try to convert it to a UIImage is wrong.

4
  • 3
    What type of image does myByteArray contain? Commented Nov 7, 2017 at 18:33
  • Could you show us how you got myByteArray, or at least the first/end bytes of data, since it's a JPEG, there should the be the corresponding StartOfFile/EndOfFile for .jpg. I don't use Xamarin, but could it be an issue of Big/LittleEndian? Commented Nov 7, 2017 at 20:16
  • @Larme it works on my Android platform so I don't think that is the issue here. Commented Nov 7, 2017 at 22:48
  • @Darius If apparently the issue was about NSAllowsArbiraryLoads was missing, then myByteArray would be nil. So clearly, checking the value of it could have point it that the issue is on the download part. Commented Nov 8, 2017 at 9:48

1 Answer 1

2

I am not sure how you are downloading the data into a byte array, but here is an example of using NSUrlConnection:

// allow NSAllowsArbitraryLoads since this is a HTTP connection
var url = new NSUrl("http://example.com/foobar.jpg");
var data = NSUrlConnection.SendSynchronousRequest(NSUrlRequest.FromUrl(url), out var response, out var error);
var image = UIImage.LoadFromData(data);
ImageView1.Image = image;

Note: This is a synchronous request, use SendAsynchronousRequest or do this off the main thread.

It was actually the NSAllowsArbiraryLoads permission that I was missing.

You can disable all ATS via NSAllowsArbitraryLoads:

<key>NSAppTransportSecurity</key>
<dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
</dict>

Preferable you can do it at the domain level:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.