简体   繁体   English

将UIImage转换为const char *?

[英]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. 我需要将UIImage转换为const char *因为我使用的是来自Google的protobuffer ,消息结构接受这种类型的参数。 I started with parsing the image into NSData : 我首先将图像解析为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. 但是,当我打印结果时,这就是我得到的: \\xff\\xd8\\xff\\xe0这是错误的,因为结果必须是带有编码字符的长字符串。

"Printing" char* aborts, when \\0 is reached. 到达\\ 0时,“打印” char*中止。 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. 您根本无法将数据作为C字符串处理,因为C字符串以0结尾。 Do not use any function that expects a C-string. 不要使用任何需要C字符串的函数。 It will fail. 它会失败。

Store a length instead and handle it as "byte data". 而是存储一个长度,并将其作为“字节数据”处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM