私は会話内のユーザーのアバターイメージを表示します。 JSQMessageViewControllerを使用したので、以下の関数を使用してこの目標を達成する必要があります。しかし、observeeventtypeはこの関数を呼び出して呼び出されないように見え、代わりにnil(MyuserImageまたはOtheruserImage)があります。クラッシュが表示されます。だから、どのようにfirebaseの異なるユーザーの写真のURLを取得し、期待されるアバター画像を返すことができますか?ありがとうございました!firebase observeeventtype 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];
}
}