2

I have a byte array that contains data for an image. The byte array is not read from an existing image file. The byte array is obtained by parsing a PDF file and reading an image embedded in it. I want to convert these bytes into an image and display it in a UIImageView. I used the following code snippet. But the UIImage.LoadFromData method returns null.

byte[] bytes = byteArray;
NSData nsdata = NSData.FromArray(bytes);
UIImage image = UIImage.LoadFromData(nsdata);

I was able to obtain a Bitmap from the same byte array and display it in Android. I know the following details about the image and bytes.

byte length: 14700 ( = 70 * 70 * 3)

image dimensions: 70 X 70

bits per component: 24 (3 bytes)

How can I create a valid UIImage from the byte array in iOS?

Saamer
  • 4,687
  • 1
  • 13
  • 55
cruzier
  • 358
  • 2
  • 14
  • copy the stream contents to a file. Use an image editor to verify that the image data is valid and what format it is in. – Jason Oct 07 '19 at 13:51
  • @Jason I already tried that. I saved the bytes into .png and .jpg files. But both of the files could not be opened. – cruzier Oct 07 '19 at 14:26
  • then it stands to reason that the image data is not valid, or is not in the format that you think it is. – Jason Oct 07 '19 at 14:30
  • use CGImage: https://learn.microsoft.com/en-us/dotnet/api/coregraphics.cgimage?view=xamarin-ios-sdk-12 – Jason Oct 07 '19 at 14:46
  • How are you performing the conversion on iOS? Are you using the PDFKit? https://stackoverflow.com/a/45001576/11104068 – Saamer Oct 07 '19 at 14:48
  • @Jason Image is working as expected when created in android but facing issues in ios only . – cruzier Oct 07 '19 at 19:18
  • No @Saamer , i don't have used PDF kit for conversion – cruzier Oct 07 '19 at 19:20
  • I understand that. That's why I suggested you look at CGImage, which is an iOS API. If you read the docs you'll see if gives you more options about how to specify the format of the source data. – Jason Oct 07 '19 at 19:20

0 Answers0