尝试将图像上传到Firebase存储和数据库时出现问题?

时间:2018-10-09 18:21:33

标签: swift xcode firebase firebase-realtime-database firebase-storage

我实际上遇到了一个我不明白的问题。为了创建此代码,我在YouTube教程的帮助下完成了自己的工作,但是开始时却出现错误:发现扩展名时遇到[发现]错误:Error Domain = PlugInKit Code = 13“查询已取消” 并且从用户获取的图像不会上载到Firebase存储或Firebase数据库。我是xcode的新手,有人可以告诉我 我为什么呢? (而且我没有收到任何错误消息),它应该将用户选择的图像保存到Firebase存储中,然后将图像的URL注册到Firebase数据库中的用户uid下。阅读并回答这个问题。这是我的代码:

import UIKit

import Firebase

class Profile: UIViewController {
    @IBOutlet weak var MyImageView: UIImageView!
    var selectedImage: UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()

        let tapGesture = UITapGestureRecognizer(target: self, action:      #selector(Profile.handleSelectProfileImageView))
        MyImageView.addGestureRecognizer(tapGesture)
        MyImageView.isUserInteractionEnabled = true
    }

    @objc func handleSelectProfileImageView(){
        let pickerController =  UIImagePickerController()
        pickerController.delegate = self
        present(pickerController, animated: true, completion: nil)
    }

    @IBAction func OkButtonTapped(_ sender: UIButton) {

        let uid = Auth.auth().currentUser!.uid
        let storageBase = Storage().reference()
        let storageRef = storageBase.child("profile_image").child(uid)

        if let profileImg = self.selectedImage, let imageData =   profileImg.jpegData(compressionQuality: 0.1) {
            storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in

                storageRef.downloadURL { (url, error) in
                    guard url != nil else {
                        // Uh-oh, an error occurred!
                        return
                    }                        // Get the download URL in Database
                    let ref = Database.database().reference()
                    let usersReference = ref.child("users")
                    let uid = Auth.auth().currentUser!.uid
                    let newUserReference = usersReference.child(uid)
                    newUserReference.setValue(["profileImageUrl": self.provideImageData])

                }
            })
        }
    }
}

extension Profile: UINavigationControllerDelegate,  UIImagePickerControllerDelegate {

    func imagePickerController(_ picker: UIImagePickerController,  didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        // Local variable inserted by Swift 4.2 migrator.
        let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)

        print("OKAY")
        if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage{
            selectedImage = image
            MyImageView.image = image
            MyImageView.setRounded()
        }
        dismiss(animated: true, completion: nil)
    }
}

extension UIImageView {
    func setRounded() {
        self.layer.cornerRadius = (self.frame.width / 2)
        self.layer.masksToBounds = true
    }
}

// Helper function inserted by Swift 4.2 migrator.
private func convertFromUIImagePickerControllerInfoKeyDictionary(_ input: [UIImagePickerController.InfoKey: Any]) -> [String: Any] {
    return Dictionary(uniqueKeysWithValues: input.map {key, value in (key.rawValue, value)})
}

0 个答案:

没有答案