与完成处理程序相结合的返回值的问题



我创建了setAvatarimage函数,以下载uiimage,然后在默认集合视图函数中返回。我可以在控制台中看到uiimage正在下载,因此返回值应该是uiimage。但是,在集合视图中没有显示图像。

func setAvatarImages(_ senderUid: String!, completionhandler: @escaping (UIImage!) -> Void) {
    let ref = DatabaseReference.users(uid: senderUid).reference()
    ref.observe(.value, with:  { (snapshot) in
        let user = User(dictionary: snapshot.value as! [String : Any])
        user.downloadProfilePicture(completion: { (image, error) in
            if image != nil {
                let profileImage = image
                print(profileImage!)
                completionhandler(profileImage!)
            } else if error != nil {
                print(error?.localizedDescription ?? "")
            }
        })
    })
}
override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
    let message = messages[indexPath.item]
    var avatarImage: JSQMessagesAvatarImage!
    setAvatarImages(message.senderUID) { (profileImage) in
        avatarImage = JSQMessagesAvatarImageFactory.avatarImage(with: profileImage, diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))
        print(avatarImage)
    }
    return avatarImage
}

我认为异步返回功能存在问题。但是我认为,赋予setAvatarimage函数一个完整的人应该解决这个问题。谁能给我一些建议,以便返回下载的图像!

编辑!

考虑Alex建议,我更改了SetAvatarimage功能。首先,我创建了一个字典:

   var profileImages = [String : UIImage]()

的目的是将所有图像与用户一起进入profile.uid。

func setAvatarImages() {
    for user in chat.users {
        print(user.fullName)
        user.downloadProfilePicture(completion: { (image, error) in
            print(image!)
            if let profileImage = image {
                self.profileImages[user.uid] = profileImage
            } else if error != nil {
                print(error?.localizedDescription ?? "")
            }
        })
    }
}

setavatarimages在ViewDidload中被调用。

要从JSQMESSAGESVIEWCONTROLLER返回默认函数中的Avatarimage,我做到了:

 override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
    let message = messages[indexPath.item]
    let avatarImage = JSQMessagesAvatarImageFactory.avatarImage(with: profileImages[message.senderUID], diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))
    return avatarImage
}

很酷的事情是它确实有效了。但是突然间,它在用户中下载图像时会崩溃。downloadProfileimage函数。每次开始崩溃之前,我唯一更改了它,我删除了控制台中的断点点以查看其工作速度……有人知道为什么它现在崩溃了。是的,我知道最好为图像添加一些缓存,但是首先必须解决此问题!

终于得到了!

我只需要在setAvatarimages函数中添加一个完整的汉格勒!

 func setAvatarImages(completion: @escaping ([String : UIImage]) -> Void) {
    for user in chat.users {
        print(user.fullName)
        user.downloadProfilePicture(completion: { (image, error) in
            print(image!)
            if let profileImage = image {
                self.profileImages[user.uid] = profileImage
                completion(self.profileImages)
            } else if error != nil {
                print(error?.localizedDescription ?? "")
            }
        })
    }
}

然后在ViewDidload中

setAvatarImages(completion: { (profileUserImages) in
        print(profileUserImages)
    })

下一站缓存!

最新更新