将照片数据转换为 nsstring 返回 nil
我允许我的应用程序的用户拍照或从他们的库中选择一张照片。选择后,我需要获取图像数据并将其转换为字符串,以便我可以将其发送到网络服务。
我当前遇到的问题是,当我将编码设置为 UTF8 时,[NSString initWithData:] 返回 nil。我需要将 XML 消息设置为 UTF8。
NSString *dataString = [[NSString alloc] initWithData:UIImageJPEGRepresentation(theImage, 1.0) encoding:NSUTF8StringEncoding];
感谢您的任何建议!
I'm allowing the users of my app to either take a pic or select one from their library. When selected I need to get the images' data and convert it to a string so I can send it to a web service.
The problem I'm currently having is that [NSString initWithData:] is returning nil when I have the encoding set to UTF8. I need to set it to UTF8 for the XML message.
NSString *dataString = [[NSString alloc] initWithData:UIImageJPEGRepresentation(theImage, 1.0) encoding:NSUTF8StringEncoding];
Thanks for any advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用 initWithData:encoding: 方法将 UIImage 转换为 NSString。此方法仅用于将字符串数据转换为 NSString(例如文本文件)。
如果您尝试将任何类型的二进制数据转换为 NSString,可以使用某种编码。 Base64 被广泛使用。此外,您的服务器应该能够解码您发送的内容。
此外,在大多数情况下,将图像发送到服务器只需像以前一样以二进制形式 POST 即可。
You can't convert an UIImage to NSString by using initWithData:encoding: method. This method is only for converting an string's data to NSString (an Text File for example).
If you are trying to convert any kind of binary data to NSString, there are some kind of encoding available. Base64 is widely used. Also your server should have the ability to decode what you've sent.
In addition, in most cases, send an image to server just need to POST it in binary as it used to be.