User Specific Avatar in JSQMessagesViewController with SDWebImage

眉间皱痕 提交于 2019-12-11 02:37:17

问题


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!...


回答1:


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!



来源:https://stackoverflow.com/questions/42864283/user-specific-avatar-in-jsqmessagesviewcontroller-with-sdwebimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!