我在XMPP的帮助下实现聊天功能,聊天工作正常。现在我正在上传图片,为此我上传图片到S3桶,上传图片到S3后,我使用XMPP方法将S3图片URL作为消息发送给其他用户。这也工作正常,但问题是它只工作,如果我留在同一屏幕上,即聊天屏幕时,图片上传。因此,如果我的图像仍在上传,我从视图控制器弹出,它被释放,我的完成处理程序没有得到调用后,上传图像到s3,因为我的sendMessage方法也没有得到调用。所以我怎么能做到这一点。我怎么能发送我的消息,并得到完成处理程序调用,即使我在不同的屏幕上,而图像上传。
这是我的代码挑选图像和上传图像
@IBAction func didPressMedia(_ sender: Any) {
ImagePickerManager().pickImage(self) { \[weak self\] pickedImage in
guard let self = self else { return }
let compressedImage = pickedImage.resizedImage(newSize: CGSize(width: 120, height: 120))
let messageId = XMPPHelper.shared.xmppStream.generateUUID
let chat = ChatListModel()
chat.roomId = ""
chat.message = ""
chat.messageId = messageId
chat.time = String(Date().currentTimeMillis())
chat.to = self.viewModel.otherUser
chat.from = self.viewModel.myJid ?? ""
chat.type = "chat/image"
chat.imageLocalPath = messageId
chat.msgDeliveryStatus = MsgDeliveryStatus.sending.rawValue
chat.isSeenSync = true
chat.isUploading = "pending"
self.viewModel.setChatDataInRealm(item: chat, isAfterPickingImage: true)
//This is completion handler which get called after image upload to s3.
self.viewModel.didImageUploaded = { [weak self] isImgUploaded , imgUrl, imgMessageId in
guard let self = self else { return }
if isImgUploaded {
print("success uploading image")
self.viewModel.sendMessage(message: imgUrl, isImage: true, messageId: imgMessageId). // send image url as message to other user
}else{
print("error uploading image")
self.viewModel.updateRealmAfterImageUpload(uploadStatus: "failed", uploadedUrl: "", messsageId: imgMessageId)
}
}
self.viewModel.uploadImage(localPath: "\(Helper.shared.getDocumentDirectoryPath(filename: messageId)).png" ,image: compressedImage, messageId: messageId)
}
}
1条答案
按热度按时间afdcj2ne1#
试试这个代码
如何致电