具有SDWebImage的JSQMessagesViewController中的用户特定头像 [英] User Specific Avatar in JSQMessagesViewController with SDWebImage

查看:271
本文介绍了具有SDWebImage的JSQMessagesViewController中的用户特定头像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为标题会建议使用JSQMessagesViewController即时通讯。 Id像每个用户有他们自己的头像[UIImage]生病得到这从FirebaseDatabase中的URL,并通过提取。 SDWebImage。

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

let message = messages [indexPath.item]

如果让messageID = message.senderId {
rootRef.child(users)。child(messageID ).observe(.value,其中:{(snapshot)in
$ b $ 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





$ b然而,每当你发送消息并重新载入表格时,重新出现,因为我假设它正在经历所有的索引路径。

下面是处理头像的dataSource的方法;

 覆盖func collectionView(_ collectionView:JSQMessagesCollectionView !, avatarImageDataForItemAt indexPath:IndexP ath!) - > JSQMessageAvatarImageDataSource! {
return ??????????????????????????????????????????????????????????





$ b

但是不能像cellForItemAt一样组织逻辑。任何人都可以协助?...非常感谢。



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

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

let placeHolderImage = UIImage(命名为:Circled User Male Filled-25)
let avatarImage = JSQMessagesAvatarImage(avatarImage:nil,highlightedImage:nil,placeholderImage:placeHolderImage)

let message = messages [indexPath.item]

如果让messageID = message.senderId {

rootRef.child(users)。child(messageID) .observe(.value,其中:{(snapshot)in
$ b $ if let profileURL =(snapshot.value as AnyObject!)![profileURL] as!String!{

让profileNSURL:NSURL = NSURL(字符串:profileURL)!

//下载头像image somehow !?
让manager:SDWebImageManager = SDWebImageManager.shared()
manager .downloadImage(with:profileNSURL as URL !, options:[],progress:{(receivedSize:Int,actualSize:Int)in
print(receivedSize,actualSize)
},completed:{(image,错误,缓存,完成,网址)
avatarImage!.avatarImage = image
})
}
})
}

return avatarImage
}

没有错误,并且打印出图像大小。然而,图像并没有从占位符图像中改变!...

解决方案

HORAY!我终于从文件中找到了......所有的地方。



您必须按照以下步骤设置现有的头像...



如此;

  let message = messages [indexPath.item] 

添加如下内容。

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

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

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

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



和Bam!各地的化身。希望这可以帮助别人!


As the title would suggest im using JSQMessagesViewController. Id like each user to have their own Avatar [UIImage] ill be getting this from a URL in FirebaseDatabase and extracting it via. SDWebImage. I am able to get this inside;

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
}

However, every time you send a message and the table is reloaded the images disappear then reappear as I'm assuming it is going through all the index paths again.

There is this below method for handling the dataSource of the Avatars;

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

but cant seem to organise the same logic as in the cellForItemAt? can anyone assist?... Many thanks.

UPDATE: - ******************************************************************

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
}

No errors with this and its printing out the image sizes. however the image is not changing from the placeholder image!...

解决方案

HORAY! Ive finally figured it out from the Docs... Of all places.

You have to initially set up the existing avatars as follows...

so under;

let message = messages[indexPath.item]

add the following.

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

Then check if Avatars exists if it doesn't, go get them! but set them for the key you have in the existing avatars. e.g. (message.senderID)

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

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

and Bam! Avatars all over the place. Hope this helps someone!

这篇关于具有SDWebImage的JSQMessagesViewController中的用户特定头像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆