JSQMessageAvatarImageFactory 错误

JSQMessageAvatarImageFactory error

由于某种原因,我遇到了崩溃,我不确定为什么。一切都正确地从 FIRBase 中提取。但是在尝试加载用户时 url 我遇到了崩溃

打印(快照)

["MediaType": TEXT, "text": Test Message, "senderId": KG7b3tDTaoMwcAQAi2JI8vjj0OB2, "senderName": user1]
Optional({
email = "user1@gmail.com";
id = KG7b3tDTaoMwcAQAi2JI8vjj0OB2;
profileImage = "gs://big-pen.appspot.com/profileImage/KG7b3tDTaoMwcAQAi2JI8vjj0OB2";
username = user1;})

我的错误

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

这是我的代码。

func observeUsers(id: String){
       FIRDatabase.database().reference().child("people").child(id).observeEventType(.Value, withBlock: { snapshot in
        print(snapshot.value)
        if let dict = snapshot.value as? [String: AnyObject] {
            let avatarUrl = dict["profileImage"] as! String

            //call the setupAvatar function
            self.avatar(avatarUrl, messageId: id)

        }
    })
}


func avatar(url: String, messageId: String){

    if DataService.dataService.currentUser != nil{
        if url != "" {
        let fileURL = NSURL(string: url )
        let data  = NSData(contentsOfURL: fileURL!)
        let image = UIImage(data:data!)  //<-----I am getting an error at this line error code: "Thread1:EXC_BAD_INSTRUCTION(code=Exc_I386_INVOP, subcode=0x0)"
        let userImg = JSQMessagesAvatarImageFactory.avatarImageWithImage(image, diameter: 30)
            avatarDict[messageId] = userImg
        }else{    
        JSQMessagesAvatarImageFactory.avatarImageWithImage(UIImage(named: "cam_w.png"), diameter: 30)
    }
    collectionView.reloadData()
    }
}

override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
    let message = messages[indexPath.item]

    return avatarDict[message.senderId]
    //return JSQMessagesAvatarImageFactory.avatarImageWithImage(UIImage(named: "cam_w.png"), diameter: 30)
}

谢谢。我已经坚持了两天了。

也许它不是有效的 URL 所以要检查它,请将您的头像功能更改为:

func avatar(url: String, messageId: String){

    if DataService.dataService.currentUser != nil{
        if let fileURL = NSURL(string: url ),
              data  = NSData(contentsOfURL: fileURL) {

        let image = UIImage(data:data!)  //<-----I am getting an error at this line error code: "Thread1:EXC_BAD_INSTRUCTION(code=Exc_I386_INVOP, subcode=0x0)"
        let userImg = JSQMessagesAvatarImageFactory.avatarImageWithImage(image, diameter: 30)
            avatarDict[messageId] = userImg
        }else{    
        JSQMessagesAvatarImageFactory.avatarImageWithImage(UIImage(named: "cam_w.png"), diameter: 30)
    }
    collectionView.reloadData()
    }
}