简体   繁体   English

上传多张图片到firebase

[英]Uploading multiple images to firebase

Just a heads up I'm new to IOS development and Swift in particular.提醒一下,我是 IOS 开发的新手,尤其是 Swift。 I have created a "Edit Profile Page" in a view controller that is attached to a regular "profile" view controller.我在附加到常规“配置文件”视图 controller 的视图 controller 中创建了一个“编辑配置文件页面”。 On this edit profile view controller, a user can change their profile picture and cover photo.在此编辑个人资料视图 controller 上,用户可以更改他们的个人资料图片和封面照片。 I am having a hard time understanding how to allow the user to upload their photos to firebase after they choose their image and click the "save button", once they click the save button I want it to upload the image and then redirect them to their regular profile view.我很难理解如何允许用户在选择图像并单击“保存按钮”后将他们的照片上传到 firebase,一旦他们单击保存按钮,我希望它上传图像然后将它们重定向到他们的常规配置文件视图。 I have read other articles and watched other tutorials but I am not able to get the result of successfully uploading both images.我已经阅读了其他文章并观看了其他教程,但我无法获得成功上传两张图片的结果。 Here is my code so far with out any calling to upload images to firebase.到目前为止,这是我的代码,没有任何调用将图像上传到 firebase。

import UIKit
import Foundation
import Firebase
import FirebaseDatabase

class NewEditProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!

    var imagePicker = UIImagePickerController()
    var imagePicked = 0

    var selectedImage1: UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()



        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = true
    }


    @IBAction func chooseImage1(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
            imagePicked = (sender as AnyObject).tag
            present(imagePicker, animated: true)
        }

        }

    @IBAction func chooseImage2(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
            imagePicked = (sender as AnyObject).tag
                present(imagePicker, animated: true)
    }

    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage

        if imagePicked == 1 {
            imageView1.image = pickedImage
        } else if imagePicked == 2 {
            imageView2.image = pickedImage
        }
        dismiss(animated: true)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true)
    }

@IBAction func saveButton(_ sender: Any) {
    }


    @IBAction func backButton(_ sender: Any) {
         self.dismiss(animated: true, completion: nil)
        }
    }

Thank you for the help, Like I said.谢谢你的帮助,就像我说的。 Swift and Firebase overall is a new concept to me. Swift 和 Firebase 总体上对我来说是一个新概念。

If i understood your question right i think you are asking how to upload an image to firebase apon the request of saving the image.如果我正确理解您的问题,我想您是在询问如何根据保存图像的请求将图像上传到 firebase。 Im hoping even though you dont have the IOS knowledge that you have the Firebase knowledge to understand the answer below.我希望即使您没有 IOS 知识,您也有 Firebase 知识来理解下面的答案。

First update your Save / upload action as below首先更新您的Save / upload操作,如下所示

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.
        }
   }
}

After that all you have to do is connect to the FIRDatabase and save it to the node.之后,您所要做的就是连接到FIRDatabase并将其保存到节点。 Note that the nodes here are under a structure made by me so you have to adjust this method according to your node strucutre or upload your strucutre and il edit the answer.请注意,此处的节点位于我制作的结构下,因此您必须根据您的节点结构调整此方法或上传您的结构并编辑答案。

@IBAction func addPost(_ sender: Any) {


 uploadMedia() { url in 
      guard let url = url else { return }
      ref?.child("Posts").childByAutoId().setValue([
                           "myImageURL" : url
                            ])
     }
  }

You could also reffer to this for further clarification您也可以参考this以获得进一步的说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM