UIImage 到字节数组 [英] UIImage to byte array
问题描述
我正在创建一个将图像上传到服务器的应用程序.它必须在 XML 上发送字节数组.如何将字节数组转换为 NSString?
I'm creating an app that uploads an image to a server. It must send the byte array on a XML. How do I get the byte array into a NSString?
谢谢!
推荐答案
您可以将 UIImage 转换为 NSData 对象,然后从中提取字节数组.下面是一些示例代码:
You can convert the UIImage to a NSData object and then extract the byte array from there. Here is some sample code:
UIImage *image = [UIImage imageNamed:@"image.png"];
NSString *byteArray = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
如果您使用的是 PNG 图片,您可以使用 UIImagePNGRepresentation 函数如上所示,或者如果您使用的是 JPEG 图像,则可以使用 UIImageJPEGRepresentation 函数.文档可在 UIImage 类参考一个>
If you are using a PNG Image you can use the UIImagePNGRepresentation function as shown above or if you are using a JPEG Image, you can use the UIImageJPEGRepresentation function. Documentation is available on the UIImage Class Reference
这篇关于UIImage 到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!