geonl
geonl

Reputation: 37

Converting UIImage into const char*?

I need to convert a UIImage into const char * because I am using protobuffer from google and structure of message accepts this type of parameter. I started with parsing the image into NSData:

UIImage *image = [UIImage imageNamed:@"sign_up_button"];
NSData *imageData =  UIImagePNGRepresentation(image);  
const char *test = (const char*)[bytesString bytes];

But when I print the result this is what i get:\xff\xd8\xff\xe0 what is wrong because the result must be a long string with encoded characters.

Upvotes: 2

Views: 794

Answers (1)

Amin Negm-Awad
Amin Negm-Awad

Reputation: 16660

"Printing" char* aborts, when \0 is reached. This is a possible content of image data. You cannot "print" it. You cannot handle the data as a C-string at all, because C-strings are 0-terminated. Do not use any function that expects a C-string. It will fail.

Store a length instead and handle it as "byte data".

Upvotes: 3

Related Questions