UIImage 到字节数组 [英] UIImage to byte array

查看:34
本文介绍了UIImage 到字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个将图像上传到服务器的应用程序.它必须在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆