gpt4 book ai didi

swift - 带有 SDWebImage 的 JSQMessagesViewController 中的用户特定头像

转载 作者:可可西里 更新时间:2023-11-01 01:23:05 24 4
gpt4 key购买 nike

正如标题所暗示的,我正在使用 JSQMessagesViewController。我希望每个用户都有自己的 Avatar [UIImage] 从 FirebaseDatabase 中的 URL 获取它并通过提取它。 SDWeb 图像。我能把这个放进去;

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell

let message = messages[indexPath.item]

if let messageID = message.senderId {
rootRef.child("users").child(messageID).observe(.value, with: { (snapshot) in

if let profileURL = (snapshot.value as AnyObject!)!["profileURL"] as! String! {
let profileNSURL: NSURL = NSURL(string: profileURL)!
cell.avatarImageView.sd_setImage(with: profileNSURL as URL)
}
})
}
return cell
}

但是,每次您发送消息并重新加载表格时,图像都会消失然后重新出现,因为我假设它会再次通过所有索引路径。

下面是处理头像数据源的方法;

override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
return ???????????????????????????????????????
}

但似乎无法组织与 cellForItemAt 中相同的逻辑?谁能帮忙?...非常感谢。

更新:- ******************************************** **********************

override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {

let placeHolderImage = UIImage(named: "Circled User Male Filled-25")
let avatarImage = JSQMessagesAvatarImage(avatarImage: nil, highlightedImage: nil, placeholderImage: placeHolderImage)

let message = messages[indexPath.item]

if let messageID = message.senderId {

rootRef.child("users").child(messageID).observe(.value, with: { (snapshot) in

if let profileURL = (snapshot.value as AnyObject!)!["profileURL"] as! String! {

let profileNSURL: NSURL = NSURL(string: profileURL)!

// download avatar image here somehow!?
let manager: SDWebImageManager = SDWebImageManager.shared()
manager.downloadImage(with: profileNSURL as URL!, options: [], progress: { (receivedSize: Int, actualSize: Int) in
print(receivedSize, actualSize)
}, completed: { (image, error, cached, finished, url) in
avatarImage!.avatarImage = image
})
}
})
}

return avatarImage
}

这和它打印出图像尺寸没有错误。然而图像并没有从占位符图像改变!...

最佳答案

万岁!我终于从文档中弄明白了……在所有地方。

您必须按如下方式初始设置现有头像...

所以在;

let message = messages[indexPath.item]

添加以下内容。

       if avatarImage?.avatarImage == nil {
avatarImage?.avatarImage = SDImageCache.shared().imageFromDiskCache(forKey: message.senderId)
}

然后检查 Avatars 是否存在,如果不存在,就去获取它们!但是将它们设置为您在现有头像中拥有的 key 。例如(message.senderID)

if image != nil {
manager.imageCache.store(image, forKey: message.senderId)

DispatchQueue.main.async
{
avatarImage!.avatarImage = image
avatarImage!.avatarHighlightedImage = image
}

砰!化身无处不在。希望这对某人有帮助!

关于swift - 带有 SDWebImage 的 JSQMessagesViewController 中的用户特定头像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42864283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com