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屋!