将图片上传到Firebase存储和数据库 [英] Uploading Image to Firebase Storage and Database
问题描述
我想将图片的下载网址放到我的Firebase数据库中。我可以将图片上传到存储中,但我无法弄清楚如何将网址与其他帖子一起存入我的数据库。感谢高级!
@IBOutlet weak var titleText:UITextField!
@IBOutlet weak var authorText:UITextField!
@IBOutlet弱var mainText:UITextView!
@IBOutlet weak var dateText:UITextField!
@IBOutlet弱var myImageView:UIImageView!
var ref:FIRDatabaseReference?
$ b $重写func viewDidLoad(){
super.viewDidLoad()
ref = FIRDatabase.database()。reference()
}
$ b重写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)
{
//完成后
}
}
jb(imagePickerController:didFinishPickingMediaWithInfo :) func imagePickerController(_ picker:UIImagePickerController,didFinishPickingMediaWithInfo info:[String:Any])
{
if let image = info [UIImagePickerControllerOriginalImage] as? UIImage
{
myImageView.image = image
}
else
{
//错误
}
self.dismiss(animated:true,completion:nil)
let storageRef = FIRStorage.storage()。reference()。child(myImage.png)
if让uploadData = UIImagePNGRepresentation(self.myImageView.image!){
storageRef.put(uploadData,元数据:nil,完成:
{
(metadata,error)in
if error != nil {
print(error)
return
}
print(metadata)
$ b $ p如何将元数据中的下载URL放入我的数据库pre $ code
@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])
// myImageURL部分是我得到一个错误的地方
$ $ $ $ $ $ $ $ $ $> 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)
}
}
}
组织您的上传
和保存
这样的funcs:
func uploadMedia(完成:@escaping(_ url:String?) - >无效){
let storageRef = FIRStorage.storage()。reference()。child(myImage.png)
if let uploadData = UIImagePNGRepresentation(self.myImageView.image!){
如果错误!= nil {
print(error)
completion(nil)
},则在
中的storageRef.put(uploadData,元数据:nil){其他{
完成((元数据.downloadURL()?。absoluteString)!))
//您上传的照片网址。
$接下来只要连接到 FIRDatabase
并保存到您的节点。
@IBAction func addPost(_ sender :Any){
if self.titleText.text!=&& self.authorText.text!=
&& self.mainText.text!=&&如果你的网站是一个网站,那么你可以使用这个网站的名称来添加你的网站。 ).setValue([
Title:titleText.text,
Article:mainText.text,
Author:authorText.text,
Date:dateText .text,
myImageURL:url!
])
}
}
}
您也可以查看这个我的回答,了解上传数据和保存网址到数据库
希望它有帮助
I want to put the download URL of images into my Firebase Database. I can upload the Image into storage but I can't figure out how to get the URL into my database with the rest of the "post". Thanks in advanced!
@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)
}
}
}
解决方案 Organize your upload
and save
funcs like this:
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.
}
}
}
Next just connect to FIRDatabase
and save it to your node.
@IBAction func addPost(_ sender: Any) {
if self.titleText.text != "" && self.authorText.text != ""
&& self.mainText.text != "" && self.dateText.text != "" {
uploadMedia() { url in
if url != nil {
ref?.child("Posts").childByAutoId().setValue([
"Title" : titleText.text,
"Article" : mainText.text,
"Author" : authorText.text,
"Date" : dateText.text,
"myImageURL" : url!
])
}
}
}
You can also see this my answer about uploading data and saving URL's to database
Hope it helps
这篇关于将图片上传到Firebase存储和数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!