I would like to convert an UIImage
into unsigned char*
. First I convert UIImage
into NSData
, then NSData
to unsigned char*
. I use the following method to convert NSData
to unsigned char*
:
unsigned char *binaryData = (unsigned char *)[imageData bytes];
The problem is that there are many many bytes in imageData
(which is an NSData
), but after the method, the binaryData is just 0x75e8000
. I have no idea why. Could anybody help me out there? And can you suggest me one way to correctly convert NSData
to unsigned char*
?
Thanks in advance!!!
I don't think you understand what a C array is. It is represented as a pointer into the start of the array. Your 0x75e8000
is the start of the array. But the array contains no information about its length; how much more data there may be is unknown.
Also, you may not understand what the bytes
method is. The bytes
method does not "convert" anything, nor does it generate a copy; it merely provides a pointer into the NSData itself. As long as the NSData exists, the bytes
points at it, but treats it as a const void*
(which you are casting to an unsigned char*
), suitable for handing to a C function.
So, the length of the C array, which you would need to tell any C function that uses this information, must be obtained separately; it is the length
of the NSData object. And if you want to extract the data, as a copy, into a separate C array, then call getBytes:length:
.