[英]Having trouble retrieving images from PubNub didReceiveMessage Function in Swift App
我正在使用JSQMessageViewController和PubNub编写Swift聊天应用程序。 我可以实时获取文本消息并正确显示它们。 但是我一直在检索图像消息,我可以毫无问题地发送图像,但是当接收者获得图像时,它就变成了NSCFString数据。 PubNub的didReceiveMessage函数中的print(message.data.message)输出为: <UIImage: 0x155d52020>, {256, 342}
,而print(message.data)的输出为: { message = "<UIImage: 0x155d52020>, {256, 342}"; subscribedChannel = aUpVlGKxjR; timetoken = 14497691787509050;}
{ message = "<UIImage: 0x155d52020>, {256, 342}"; subscribedChannel = aUpVlGKxjR; timetoken = 14497691787509050;}
{ message = "<UIImage: 0x155d52020>, {256, 342}"; subscribedChannel = aUpVlGKxjR; timetoken = 14497691787509050;}
有人知道如何将这些数据转换为UIImage吗?
您需要将UIImage转换为base64编码,然后发送到pubnub消息,然后将base64解码为UIImage。
编码:
let imageData = UIImagePNGRepresentation(image)
let imageString = imageData.base64EncodedStringWithOptions(.allZeros)
解码:
let imageData = NSData(base64EncodedString: imageString, options: NSDataBase64DecodingOptions.fromRaw(0)!)
var image = UIImage(data: imageData)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.