【问题标题】:Firebase for iOS error when uploading an image: object does not exist上传图片时 iOS 的 Firebase 错误:对象不存在
【发布时间】:2017-07-05 18:33:17
【问题描述】:

我正在尝试将图像上传到 Firebase 存储,但在从照片库中选择图像后并在单击“选择”按钮之前出现此错误:

“创建未知类型的图像格式是错误的”

点击“上传”按钮后,我也得到“对象不存在”。

这是我的代码:

import UIKit
import Firebase
import FirebaseAuth
import FirebaseStorage
import FirebaseDatabase

class uploadVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    @IBOutlet weak var imgPost: UIImageView!
    @IBOutlet weak var txtPost: UITextView!
    var uuid = NSUUID().uuidString

    override func viewDidLoad() {
        super.viewDidLoad()

        imgPost.isUserInteractionEnabled = true
        let gestureRecognizer = UITapGestureRecognizer(target: self, 
        action: #selector(uploadVC.selectImage))
        imgPost.addGestureRecognizer(gestureRecognizer)
    }

    func selectImage() {
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.sourceType = .photoLibrary
        picker.allowsEditing = true
        present(picker, animated: true, completion: nil)
    }

    func imagePickerController(_ picker: UIImagePickerController, 
        didFinishPickingMediaWithInfo info: [String : Any]) {
        imgPost.image = info[UIImagePickerControllerEditedImage] as? 
        UIImage
        self.dismiss(animated: true, completion: nil)
    }

    @IBAction func btnUpload(_ sender: Any) {
        let mediaFolder = Storage().reference().child("media")
        if let data = UIImageJPEGRepresentation(imgPost.image!, 0.5) {
            mediaFolder.child("\(uuid).jpg").putData(data, metadata: nil, 
            completion: { (metadata, error) in
                if error != nil {
                    let alert = UIAlertController(title: "Error", message: 
                    error?.localizedDescription, preferredStyle: 
                    UIAlertControllerStyle.alert)
                    let ok = UIAlertAction(title: "OK", style: 
                    UIAlertActionStyle.cancel, handler: nil)
                    alert.addAction(ok)
                    self.present(alert, animated: true, completion: nil)
                } else {
                    print(metadata?.downloadURL()?.absoluteString)
                }
            })
        }
    }
}

【问题讨论】:

    标签: ios swift firebase firebase-storage


    【解决方案1】:

    我刚刚弄清楚它是如何工作的。 根据 swift 3 的 Firebase 存储示例,存储首先需要身份验证。

    参考:Firebase GitHub example

    import UIKit
    import Photos
    import Firebase
    import FirebaseAuth
    import FirebaseStorage
    import FirebaseDatabase
    
    class uploadVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
        @IBOutlet weak var imgPost: UIImageView!
        @IBOutlet weak var txtPost: UITextView!
    
        var uuid = NSUUID().uuidString
        var storageRef: StorageReference!
        var imageFile: URL?
        var filePath: String?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // [START configurestorage]
            storageRef = Storage.storage().reference()
            // [END configurestorage]
    
            // [START storageauth]
            // Using Cloud Storage for Firebase requires the user be authenticated. Here we are using
            // anonymous authentication.
            if Auth.auth().currentUser == nil {
                Auth.auth().signInAnonymously(completion: { (user: User?, error: Error?) in
                    if let error = error {
                        let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
                        let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
                        alert.addAction(ok)
                        self.present(alert, animated: true, completion: nil)
                    }
                })
            }
            // [END storageauth]
    
            imgPost.isUserInteractionEnabled = true
            let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(uploadVC.selectImage))
            imgPost.addGestureRecognizer(gestureRecognizer)
        }
    
        func selectImage() {
            let picker = UIImagePickerController()
            picker.delegate = self
            picker.sourceType = .photoLibrary
            picker.allowsEditing = true
            present(picker, animated: true, completion: nil)
        }
    
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
            imgPost.image = info[UIImagePickerControllerEditedImage] as? UIImage
            picker.dismiss(animated: true, completion:nil)
            if #available(iOS 8.0, *), let referenceUrl = info[UIImagePickerControllerReferenceURL] as? URL {
                let assets = PHAsset.fetchAssets(withALAssetURLs: [referenceUrl], options: nil)
                let asset = assets.firstObject
                asset?.requestContentEditingInput(with: nil, completionHandler: { (contentEditingInput, info) in
                    self.imageFile = contentEditingInput?.fullSizeImageURL
                    self.filePath = "media/" + "\(self.uuid).jpg"
                })
            }
        }
    
        @IBAction func btnUpload(_ sender: Any) {
            // [START uploadimage]
            self.storageRef.child(filePath!)
                .putFile(from: imageFile!, metadata: nil) { (metadata, error) in
                    if let error = error {
                        let alert = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
                        let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
                        alert.addAction(ok)
                        self.present(alert, animated: true, completion: nil)
                        return
                    }
                    print(metadata?.downloadURL()?.absoluteString ?? "nothing")
            }
            // [END uploadimage]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-13
      • 2017-07-03
      • 2017-05-03
      • 2018-01-12
      • 2017-04-15
      • 1970-01-01
      • 2021-06-28
      • 2023-02-16
      相关资源
      最近更新 更多