ios - 在 Swift App 中从 PubNub didReceiveMessage 函数检索图像时遇到问题

标签 ios swift uiimage pubnub nscfstring

我正在使用 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;} 有谁知道如何将这些数据转换为 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)

引用:Convert between UIImage and Base64 string

关于ios - 在 Swift App 中从 PubNub didReceiveMessage 函数检索图像时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34208443/

相关文章:

ios - Swift iOS - 标签 Collection View

ios - 来自 XIB 文件 : height is NULL 的 UIView

ios - 尽管在 Swift 中使用 prepareToPlay(),AVAudioPlayer 仍会产生滞后

ios - 将 UIImage View 转换为 NSURL 以将其上传到 Firebase

ios - 并发魔术记录

ios - Swift 4 - 测验应用程序随机问题

swift - NSGridview 的背景色

ios7 - UIImagePickerController 图像类型

ios - UIImage imageWithData 返回 nil

ios - 一起识别长按和平移手势识别器