Transform Uiimage into Byte Array

Asked

Viewed 51 times

0

I need to transform an image into an array of bytes, as I do to transform this image into an Array Bytes?

  • Question: Do you want an array of bytes that represents a JPEG or PNG file, as the @carlosfigueira answer illustrated, or do you want an array of bytes with the framebuffer of the image itself? If his answer is correct, it would be nice to mention this in the question.

1 answer

1


You can use the functions Uiimagejpegrepresentation or Uiimagepngrepresentation to cover your image in a NSData. From there you can allocate an array of bytes the size of the data, and copy the bytes to it, as in the example below.

UIImage *image = [UImage imageNamed:@"foto.png"];
NSData *data = UIImagePNGRepresentation(image);
NSUInteger len = [data length];
Byte *bytes = (Byte*)malloc(len);
memcpy(bytes, [data bytes], len); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.