我正在尝试使用swift
与firebase
构建聊天应用并收到此错误:下面是代码:我仍然无法理解错误!
我也在使用JSQMessageViewController
。
func sendMedia(picture: UIImage?, video: NSURL?) {
print(picture)
print(FIRStorage.storage().reference())
if let picture = picture {
let filePath = "\(FIRAuth.auth()!.currentUser?.uid)/\(NSDate.timeIntervalSinceReferenceDate())"
print(filePath)
let data = UIImageJPEGRepresentation(picture, 0.1)
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpg"
FIRStorage.storage().reference().child(filePath).putData(data!, metadata: metadata) { (metadata, error) in
if error != nil {
print(error?.localizedDescription)
return
}
let fileUrl = metadata!.downloadURLs![0].absoluteString
let newMessage = self.messageRef.childByAutoId()
let messageData = ["fileUrl": fileUrl, "senderId": self.senderId, "senderName": self.senderDisplayName, "MediaType": "PHOTO"]
newMessage.setValue(messageData)
}
}else if let video = video {
let filePath = "\(FIRAuth.auth()!.currentUser?.uid)/\(NSDate.timeIntervalSinceReferenceDate())"
print(filePath)
let data = NSData(contentsOfURL: video)
let metadata = FIRStorageMetadata()
metadata.contentType = "video/mp4"
FIRStorage.storage().reference().child(filePath).putData(data!, metadata: metadata) { (metadata, error) in
if error != nil {
print(error?.localizedDescription)
return
}
let fileUrl = metadata!.downloadURLs![0].absoluteString
let newMessage = self.messageRef.childByAutoId()
let messageData = ["fileUrl": fileUrl, "senderId": self.senderId, "senderName": self.senderDisplayName, "MediaType": "VIDEO"]
newMessage.setValue(messageData)
}
}
}
}
extension ChatViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
print("Did finish picking")
//get the image
print(info)
if let picture = info[UIImagePickerControllerOriginalImage] as? UIImage {
let photo = JSQPhotoMediaItem(image: picture)
messages.append(JSQMessage(senderId: senderId, displayName: senderDisplayName, media: photo))
sendMedia(picture, video: nil)
}
else if let video = info[UIImagePickerControllerMediaURL] as? NSURL {
let videoItem = JSQVideoMediaItem(fileURL: video, isReadyToPlay: true)
messages.append(JSQMessage(senderId: senderId, displayName: senderDisplayName, media: videoItem))
sendMedia(nil, video: NSURL)
}
self.dismissViewControllerAnimated(true, completion: nil)
collectionView.reloadData()
}
}
如果错误是NSURl的类型,我无法弄清楚如何解决它。
答案 0 :(得分:1)
在这一行:
sendMedia(nil, video: NSURL)
您正在传递NSURL
作为第二个参数video
。
(请记住video:
只是一个参数标签。)
也许你需要把它写成:
sendMedia(nil, video: video)