我有viewDidLoad
中调用的头像函数(如果它有帮助,这是函数,虽然我认为问题不是函数本身:https://pastebin.com/ksHmucTX)。
在viewDidLoad
我称之为:
createAvatar(senderId: self.senderId, senderDisplayName: self.senderDisplayName, user: self.currentUser!, color: UIColor.lightGray)
但由于我只传递了我的信息(作为当前用户),因此在使用该应用时,我只能看到我的头像,而不是聊天中其他人的头像。
在avatarImageDataForItemAt
函数中,有一个索引路径可以确定哪个消息是:
override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
let message = messages[indexPath.row]
return avatars[message.senderId]
}
所以我想知道如何在viewDidLoad中获取正确的senderId
(即发送消息的人的ID),以便我可以将其插入createAvatars
调用,而不是我现在有,senderId
只有我的身份证 - 这样每个人的头像都会出现,而不仅仅是我的(或者是当前用户)。
感谢您的帮助!
编辑:
func getParticipantInfo() {
let groupRef = databaseRef.child("groups").child(currentRoomIdGlobal)
groupRef.observe(.childAdded, with: { snapshot in
if let snapDict = snapshot.value as? [String : AnyObject] {
for each in snapDict {
let uid = each.key
let avatar = each.value["profilePicture"] as! String
let gender = each.value["gender"] as! String
let handle = each.value["handle"] as! String
let name = each.value["name"] as! String
let status = each.value["status"] as! String
// Set those to the dictionaries [UID : value]
self.avatarDictionary.setValue(avatar, forKey: uid)
self.nameDictionary.setValue(name, forKey: uid)
self.genderDictionary.setValue(gender, forKey: uid)
self.handleDictionary.setValue(handle, forKey: uid)
self.statusDictionary.setValue(status, forKey: uid)
DispatchQueue.main.async {
self.createAvatar(senderId: uid, senderDisplayName: name, photoUrl: avatar, color: UIColor.lightGray)
self.navCollectionView?.collectionView?.reloadData()
}
}
}
})
}
createAvatar:
func createAvatar(senderId: String, senderDisplayName: String, photoUrl: String, color: UIColor) {
if self.avatars[senderId] == nil {
let img = MyImageCache.sharedCache.object(forKey: senderId as AnyObject) as? UIImage
if img != nil {
self.avatars[senderId] = JSQMessagesAvatarImageFactory.avatarImage(with: img, diameter: 30)
} else if photoUrl != "" {
// the images are very small, so the following methods work just fine, no need for Alamofire here
if photoUrl.contains("https://firebasestorage.googleapis.com") {
self.storage.reference(forURL: photoUrl).data(withMaxSize: 1 * 1024 * 1024) { (data, error) -> Void in
if (error != nil) {
assertionFailure("Error with Firebase Storage")
}
else {
let newImage = UIImage(data: data!)
self.avatars[senderId] = JSQMessagesAvatarImageFactory.avatarImage(with: newImage, diameter: 30)
MyImageCache.sharedCache.setObject(newImage!, forKey: senderId as AnyObject, cost: data!.count)
}
}
} else if let data = NSData(contentsOf: NSURL(string:photoUrl)! as URL) {
let newImage = UIImage(data: data as Data)!
self.avatars[senderId] = JSQMessagesAvatarImageFactory.avatarImage(with: newImage, diameter: 30)
MyImageCache.sharedCache.setObject(newImage, forKey: senderId as AnyObject, cost: data.length)
} else {
// Initials
let senderName = nameDictionary.value(forKey: senderId) as! String
let initials = senderName.components(separatedBy: " ").reduce("") { ($0 == "" ? "" : "\($0.characters.first!)") + "\($1.characters.first!)" }
let placeholder = JSQMessagesAvatarImageFactory.avatarImage(withUserInitials: initials, backgroundColor: UIColor.gray, textColor: UIColor.white, font: UIFont.systemFont(ofSize: 14), diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))
MyImageCache.sharedCache.setObject(placeholder!, forKey: senderId as AnyObject)
}
}
} else {
// Initials
let senderName = nameDictionary.value(forKey: senderId) as! String
let initials = senderName.components(separatedBy: " ").reduce("") { ($0 == "" ? "" : "\($0.characters.first!)") + "\($1.characters.first!)" }
let placeholder = JSQMessagesAvatarImageFactory.avatarImage(withUserInitials: initials, backgroundColor: UIColor.gray, textColor: UIColor.white, font: UIFont.systemFont(ofSize: 14), diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))
MyImageCache.sharedCache.setObject(placeholder!, forKey: senderId as AnyObject)
}
}
答案 0 :(得分:1)
据我所知,JSQMessage包含发件人的senderId,因此下面的函数已经指定了要返回的头像:
override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
let message = messages[indexPath.row]
return avatars[message.senderId]
}
您需要添加头像
var avatars = [String: JSQMessagesAvatarImage]()
我假设您拥有该用户的userId,因此每次有人加入对话时都以相同的方式获取头像。让我们说userToChatWith:
createAvatar(userToChatWith.userKey, senderDisplayName: userToChatWithName, user: userToChatWith, color: UIColor.lightGray)