@IBOutlet weak var TitleText: UITextFIEld!@IBOutlet weak var authorText: UITextFIEld!@IBOutlet weak var mainText: UITextVIEw!@IBOutlet weak var dateText: UITextFIEld!@IBOutlet weak var myImageVIEw: UIImageVIEw!var ref:FIRDatabaseReference?overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() ref = FIRDatabase.database().reference()}overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning()}@IBAction func uploadImage(_ sender: Any) { let image = UIImagePickerController() image.delegate = self image.sourceType = UIImagePickerControllerSourceType.photolibrary image.allowsEditing = false self.present(image,animated: true) { //after its completed }}@objc(imagePickerController:dIDFinishPickingMediawithInfo:) func imagePickerController(_ picker: UIImagePickerController,dIDFinishPickingMediawithInfo info: [String : Any]){ if let image = info[UIImagePickerControllerOriginalimage] as? UIImage { myImageVIEw.image = image } else { //error } self.dismiss(animated: true,completion: nil) let storageRef = Firstorage.storage().reference().child("myImage.png") if let uploadData = UIImagePNGRepresentation(self.myImageVIEw.image!){ storageRef.put(uploadData,Metadata: nil,completion: { (Metadata,error) in if error != nil { print("error") return } print(Metadata) //how do I put the download URL in the Metadata into my database } ) }}@IBAction func addPost(_ sender: Any) { if self.TitleText.text != "" && self.authorText.text != "" && self.mainText.text != "" && self.dateText.text != "" { ref?.child("posts").childByautoID().setValue(["Title": TitleText.text,"Article": mainText.text,"Author": authorText.text,"Date": dateText.text,"myImageURL": myImageURL]) //the myImageURL part is where I get an error self.performSegue(withIDentifIEr: "post",sender: self) } else{ let alertController = UIAlertController(Title: "Oops!",message: "FIEld left blank",preferredStyle: .alert) let defaultAction = UIAlertAction(Title: "Ok",style: .cancel,handler: nil) alertController.addAction(defaultAction) self.present(alertController,animated: true,completion: nil) } }}组织您的上传并保存这样的功能:
func uploadMedia(completion: @escaPing (_ url: String?) -> VoID) { let storageRef = Firstorage.storage().reference().child("myImage.png") if let uploadData = UIImagePNGRepresentation(self.myImageVIEw.image!) { storageRef.put(uploadData,Metadata: nil) { (Metadata,error) in if error != nil { print("error") completion(nil) } else { completion((Metadata?.downloadURL()?.absoluteString)!)) // your uploaded photo url. } } }
接下来只需连接到FIRDatabase并将其保存到您的节点.
@IBAction func addPost(_ sender: Any) { if self.TitleText.text != "" && self.authorText.text != "" && self.mainText.text != "" && self.dateText.text != "" { uploadMedia() { url in guard let url = url else { return } ref?.child("posts").childByautoID().setValue([ "Title" : TitleText.text,"Article" : mainText.text,"Author" : authorText.text,"Date" : dateText.text,"myImageURL" : url! ]) } }
您还可以查看有关上载数据和将URL保存到数据库的my answer
希望能帮助到你
总结以上是内存溢出为你收集整理的swift – 将图像上传到Firebase存储和数据库全部内容,希望文章能够帮你解决swift – 将图像上传到Firebase存储和数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)