Swift 4 上传图片到 Firebase
Swift 4 uploading images to Firebase
大家下午好,我正在尝试将图像上传到 firebase 并在单元格上显示。除了图像之外,我几乎所有内容都在工作(我无法消除错误)。
Here is the error I am getting
Cannot invoke initializer for type 'Posts' with an argument list of type '(postImageStringUrl: String, content: String!, postId: String)'
//[Save Image]
// Create data in the server
let data = UIImageJPEGRepresentation(self.addedImage.image!, 0.5)
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
let postId = "\(Auth.auth().currentUser!.uid)\(NSUUID().uuidString)"
// Create a reference to the file you want to upload
let imagePath = "postImages\(postId)/postPic.jpg"
storageRef.child(imagePath).putData(data!, metadata: metadata) { (metadata, error) in
if error == nil {
let postRef = self.databaseRef.child("posts").childByAutoId()
let post = Posts(postImageStringUrl: String (describing: metadata!.downloadURL()), content: descriptionTextView.text, postId: postId)
postRef.setValue(post.toAnyObject())
}else{
print(error.debugDescription)
}
}
//[Save Image]
这是我的 postViewController
导入基金会
导入 Firebase
导入 Firebase 数据库
导入 FirebaseStorage
结构帖子{
var postImageStringUrl: String!
var department: String!
var content: String!
var username: String!
var postId: String!
var ref: DatabaseReference?
var key: String!
init(postImageStringUrl: String, department: String, content: String, username: String,postId: String, key: String = ""){
self.postImageStringUrl = postImageStringUrl
self.department = department
self.content = content
self.username = username
self.postId = postId
self.key = key
self.ref = Database.database().reference()
}
init(snapshot: DataSnapshot){
let snapshotValue = snapshot.value as! NSMutableDictionary
self.postImageStringUrl = snapshotValue["postImageStringUrl"] as! String
self.department = snapshotValue["department"] as! String
self.content = snapshotValue["content"] as! String
self.username = snapshotValue["username"] as! String
self.postId = snapshotValue["postId"] as! String
self.key = snapshot.key
self.ref = snapshot.ref
}
func toAnyObject() -> [String: AnyObject] {
return ["postImageStringUrl": postImageStringUrl as AnyObject, "department": department as AnyObject,"content": content as AnyObject,"username": username as AnyObject, "postId": postId as AnyObject]
}
}
任何帮助将不胜感激...Ty
您的帖子初始为
init(
postImageStringUrl: String,
department: String,
content: String,
username: String,
postId: String,
key: String = "")
您似乎正在尝试使用
对其进行初始化
let post = Posts(
postImageStringUrl: String (describing: metadata!.downloadURL()),
content: descriptionTextView.text,
postId: postId)
基本上只是在初始化中缺少几个参数;部门、用户名
大家下午好,我正在尝试将图像上传到 firebase 并在单元格上显示。除了图像之外,我几乎所有内容都在工作(我无法消除错误)。
Here is the error I am getting
Cannot invoke initializer for type 'Posts' with an argument list of type '(postImageStringUrl: String, content: String!, postId: String)'
//[Save Image]
// Create data in the server
let data = UIImageJPEGRepresentation(self.addedImage.image!, 0.5)
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
let postId = "\(Auth.auth().currentUser!.uid)\(NSUUID().uuidString)"
// Create a reference to the file you want to upload
let imagePath = "postImages\(postId)/postPic.jpg"
storageRef.child(imagePath).putData(data!, metadata: metadata) { (metadata, error) in
if error == nil {
let postRef = self.databaseRef.child("posts").childByAutoId()
let post = Posts(postImageStringUrl: String (describing: metadata!.downloadURL()), content: descriptionTextView.text, postId: postId)
postRef.setValue(post.toAnyObject())
}else{
print(error.debugDescription)
}
}
//[Save Image]
这是我的 postViewController
导入基金会 导入 Firebase 导入 Firebase 数据库 导入 FirebaseStorage结构帖子{
var postImageStringUrl: String! var department: String! var content: String! var username: String! var postId: String! var ref: DatabaseReference? var key: String! init(postImageStringUrl: String, department: String, content: String, username: String,postId: String, key: String = ""){ self.postImageStringUrl = postImageStringUrl self.department = department self.content = content self.username = username self.postId = postId self.key = key self.ref = Database.database().reference() } init(snapshot: DataSnapshot){ let snapshotValue = snapshot.value as! NSMutableDictionary self.postImageStringUrl = snapshotValue["postImageStringUrl"] as! String self.department = snapshotValue["department"] as! String self.content = snapshotValue["content"] as! String self.username = snapshotValue["username"] as! String self.postId = snapshotValue["postId"] as! String self.key = snapshot.key self.ref = snapshot.ref } func toAnyObject() -> [String: AnyObject] { return ["postImageStringUrl": postImageStringUrl as AnyObject, "department": department as AnyObject,"content": content as AnyObject,"username": username as AnyObject, "postId": postId as AnyObject] }
}
任何帮助将不胜感激...Ty
您的帖子初始为
init(
postImageStringUrl: String,
department: String,
content: String,
username: String,
postId: String,
key: String = "")
您似乎正在尝试使用
对其进行初始化let post = Posts(
postImageStringUrl: String (describing: metadata!.downloadURL()),
content: descriptionTextView.text,
postId: postId)
基本上只是在初始化中缺少几个参数;部门、用户名