如何快速将图像上传到Firestore

时间:2020-01-13 12:46:44

标签: swift firebase google-cloud-firestore

嗨,如何将从图库中选择的图像上传到Firestore?

import UIKit
import FirebaseStorage

class HomeViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var image: UIImageView!

    let imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()
        image.makeRoundCorners(byRadius: 20)
        image.image = UIImage(named: ("upload")) //Default image
    }

    @IBAction func choose(_ sender: Any) {
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
        self.present(imagePicker, animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
           if let imageV = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
               image.image = imageV
        }
           dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
    {
        picker.dismiss(animated: true, completion:nil)
    }

    @IBAction func upload(_ sender: Any) {


    }
}

extension UIImageView {

   func makeRoundCorners(byRadius rad: CGFloat) {
      self.layer.cornerRadius = rad
      self.clipsToBounds = true
   }
}

我想将其上传到Firestore,以便能够检索图像的url,然后进行渲染。使用此代码,我可以让用户从画廊中选择图像,但是例如,当我按下上载按钮时,如何上载该图像呢? 谢谢 !

1 个答案:

答案 0 :(得分:1)

首先导入FirebaseFirestore。

import FirebaseFirestore

然后,您可以访问FireStore,首先(1)将图像上传到存储,然后(2)获取URL以访问图像。

let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"

let storage = Storage.storage().reference()
storage.child("images").putData(imageV.jpegData(compressionQuality: 0.4), metadata: metadata) { meta, error in
    if let error = error {
        print(error)
        return
    }

    storage.child(folder).downloadURL { url, error in
        if let error = error {
            // Handle any errors
            print(error)
        }
    }
}

我喜欢对UIImage进行扩展:

extension UIImage {
    func upload(with folder: String, completion: @escaping (URL?) -> Void) {
        let metadata = StorageMetadata()
        metadata.contentType = "image/jpeg"

        //let fileName = [UUID().uuidString, String(Date().timeIntervalSince1970)].joined()
        let data = self.jpegData(compressionQuality: 0.4)
        let storage = Storage.storage().reference()
        storage.child(folder).putData(data, metadata: metadata) { meta, error in
            if let error = error {
                print(error)
                completion(nil)
                return
            }

            storage.child(folder).downloadURL { url, error in
                if let error = error {
                    // Handle any errors
                    print(error)
                    completion(nil)
                }
                else {
                    completion(url)
                }
            }
        }
    }
}

然后称呼它

imageV.upload(with: "images", completion: { (url: URL?) in
    print(url)
}