我正在使用JSQMessage库和Firebase后端服务制作聊天应用。当用户发送大量媒体消息(如图像或视频)时,我遇到了问题。我也使用SDWebImage库但它无法解决问题。当我在模拟器中运行我的应用程序时,集合视图可以顺利滚动,但是当我在真实设备中尝试时,它很糟糕。所以请帮我解决这个问题。感谢您的贡献。
这是我获取数据的代码
databaseReference.child("Message").child(fromId).child(toId).queryLimited(toLast: UInt(numberMess)).observe(.childAdded, with: { (snapshot) in
if let dict = snapshot.value as? [String : Any] {
let receiverId = dict["ReceiverId"] as? String ?? ""
let senderId = dict["SenderId"] as? String ?? ""
let time = dict["Time"] as? NSNumber ?? 0
let mediatype = dict["MediaType"] as? String ?? ""
let status = dict["Status"] as? String ?? ""
let sendername = dict["SenderName"] as? String ?? ""
let receivername = dict["ReceiverName"] as? String ?? ""
if mediatype == "Text" {
let text = dict["Text"] as! String
let newJSQMess = JSQMessage(senderId: senderId, displayName: sendername, text: text)
let newMess = Messages(TextString: text, SenderIdString: senderId, SenderNameString: sendername, ReceiverNameString: receivername, ReceiverIdString: receiverId, TimeNumber: time)
if newMess.SenderId == self.senderId! {
newMess.ChatId = newMess.ReceiverId
} else {
newMess.ChatId = newMess.SenderId
}
newMess.Status = status
if newMess.ChatId == self.receiverId! {
self.Array_Messages.append(newJSQMess!)
self.ClassMessages.append(newMess)
DispatchQueue.main.async {
self.finishReceivingMessage()
self.collectionView.reloadData()
}
}
}
else if mediatype == "Photo" {
let photo = JSQPhotoMediaItem(image: nil)
let PhotoUrl = dict["PhotoUrl"] as? String ?? ""
let downloader = SDWebImageDownloader.shared()
downloader.downloadImage(with: URL(string: PhotoUrl), options: [], progress: nil, completed: { (image, data, error, finished) in
DispatchQueue.main.async(execute: {
photo?.image = image
self.collectionView.reloadData()
})
})
self.showMediaData(senderId: senderId, displayName: sendername, text: "", ReceiverId: receiverId, Receivername: receivername, TimeNumber: time, media: photo!, Status: status, PhotoUrlString: PhotoUrl, ThumnailUrlString: "", VideoUrlString: "", StatusString: status)
}
else if mediatype == "Video" {
let thumnailImage = dict["ThumnailImageUrl"] as! String
let thumnailImageUrl = URL(string: thumnailImage)
let data = try? Data(contentsOf: thumnailImageUrl!)
let videoUrl = dict["VideoUrl"] as? String ?? ""
let video = URL(string: videoUrl)
let videoItem = JSQVideoMediaItem(fileURL: video!, isReadyToPlay: true, thumbnailImage: UIImage.sd_image(with: data!))
self.showMediaData(senderId: senderId, displayName: sendername, text: "", ReceiverId: receiverId, Receivername: receivername, TimeNumber: time, media: videoItem, Status: status, PhotoUrlString: "", ThumnailUrlString: thumnailImage, VideoUrlString: videoUrl, StatusString: status)
}
}
})
// show media in chat log
private func showMediaData(senderId: String, displayName: String, text: String, ReceiverId: String, Receivername : String, TimeNumber: NSNumber, media: JSQMediaItem, Status : String, PhotoUrlString : String, ThumnailUrlString : String, VideoUrlString : String, StatusString : String) {
let newJSQMedia = JSQMessage(senderId: senderId, displayName: displayName, media: media)
let newMess = Messages(TextString: text, SenderIdString: senderId, SenderNameString: displayName, ReceiverNameString: Receivername, ReceiverIdString: ReceiverId, TimeNumber: TimeNumber)
if newMess.SenderId == self.senderId! {
newMess.ChatId = newMess.ReceiverId
media.appliesMediaViewMaskAsOutgoing = true
} else {
newMess.ChatId = newMess.SenderId
media.appliesMediaViewMaskAsOutgoing = false
}
newMess.PhotoUrl = PhotoUrlString
newMess.Status = StatusString
if newMess.ChatId == self.receiverId! {
self.Array_Messages.append(newJSQMedia!)
self.ClassMessages.append(newMess)
self.finishReceivingMessage()
DispatchQueue.main.async() {
self.collectionView.reloadData()
}
}
}
答案 0 :(得分:0)
我找到了我的bug的解决方案。看来collectionView重新加载了很多时间取决于发送消息的数量。因此解决方案非常简单,只需删除collectionView重新加载数据
即可