Ich werde Avatar Bild von Benutzern in einer Konversation zeigen. Ich habe JSQMessageViewController verwendet, daher sollte die folgende Funktion verwendet werden, um dieses Ziel zu erreichen. Allerdings scheint observateventtype diese Funktion herauszuholen und nicht aufgerufen zu werden, und es gibt eine Null (MyuserImage oder OtheruserImage) als Gegenleistung. So wird ein Crash erscheinen. Wie kann ich die Foto-URL verschiedener Benutzer in Firebase abrufen und dann das erwartete Avatarbild zurückgeben? Vielen Dank!firebase observateventtype jsqmessagecollectionview avatar
- (id<JSQMessageAvatarImageDataSource>)collectionView:(JSQMessagesCollectionView *)collectionView avatarImageDataForItemAtIndexPath:(NSIndexPath *)indexPath
{
JSQMessage *message = [self.msgArray objectAtIndex:indexPath.item];
if([message.senderId isEqualToString:self.senderId]){
NSString *MyuserId = [FIRAuth auth].currentUser.uid;
__block NSString *MyuserImage;
NSLog(@"uid is : %@",MyuserId);
[[_photoRef child:@"users"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
NSLog(@"My key is : %@",snapshot.key);
if([snapshot.key isEqualToString:MyuserId]){
NSLog(@"snapshot value is : %@", snapshot.value);
MyuserImage = snapshot.value[@"photo"];
}
}];
NSURL *url = [NSURL URLWithString:MyuserImage];
NSData *data = [NSData dataWithContentsOfURL:url];
self.myuserImage= [[UIImage alloc] initWithData:data];
return [JSQMessagesAvatarImageFactory avatarImageWithImage:self.myuserImage diameter:15];
}
else{
NSString *OtheruserId = message.senderId;
__block NSString *OtheruserImage;
NSLog(@"other userId is: %@",OtheruserId);
[[_photoRef child:@"users"]observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) {
NSLog(@"other user's key is: %@", snapshot.key);
if([snapshot.key isEqualToString:OtheruserId]){
NSLog(@"snapshot value is: %@", snapshot.value);
OtheruserImage = snapshot.value[@"photo"];
}
}];
NSURL *url = [NSURL URLWithString:OtheruserImage];
NSData *data = [NSData dataWithContentsOfURL:url];
self.otheruserImage= [[UIImage alloc] initWithData:data];
return [JSQMessagesAvatarImageFactory avatarImageWithImage:self.otheruserImage diameter:15];
}
}