gpt4 book ai didi

ios - JSQMessagesViewController:如何在 viewDidLoad 中获取 senderId?

转载 作者:行者123 更新时间:2023-11-28 15:38:20 26 4
gpt4 key购买 nike

我有一个在 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 只是我的 ID - 从而使每个人的头像都出现,而不仅仅是我的(或当前用户的任何人)。

感谢您的帮助!

编辑:

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()
}
}
}
})
}

创建头像:

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)
}
}

最佳答案

据我所知,JSQMessage中包含发送者的senderId,所以下面的函数已经指定了要返回什么头像:

override func collectionView(_ collectionView: JSQMessagesCollectionView!,    avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource!    {
let message = messages[indexPath.row]
return avatars[message.senderId]
}

您需要添加头像

var avatars = [String: JSQMessagesAvatarImage]()

我假设您拥有该用户的用户 ID,因此每次有人加入对话时都以相同的方式获取头像。让我们说 userToChatWith:

    createAvatar(userToChatWith.userKey, senderDisplayName: userToChatWithName,  user: userToChatWith, color: UIColor.lightGray)

关于ios - JSQMessagesViewController:如何在 viewDidLoad 中获取 senderId?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44057085/

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