iOS Firebase存储文件上传-对象不存在 [英] IOS Firebase storage file upload - object does not exist

查看:66
本文介绍了iOS Firebase存储文件上传-对象不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在IOS(Swift)上使用Firebase将用户选择的图像文件上传到Firebase存储. 我已经对用户进行了身份验证,所以这不是问题. 在下面,您可以看到我要使用的视图控制器,但是当它尝试上传图像时,我会收到一条错误消息,提示:

I'm trying to use Firebase on IOS (Swift) to upload an image file chosen by the user to the firebase storage. I've already authenticated the user, so that is not the problem. Below you can see the view controller that I'm trying to use, but when it tries to upload the image, I get back an error message saying:

对象图像/vesVLmkqS2cCLQTJOjv9CFe8mh22/0524729A-855E-4E63-8C11-50F4C4B1A905不存在.

Object images/vesVLmkqS2cCLQTJOjv9CFe8mh22/0524729A-855E-4E63-8C11-50F4C4B1A905 does not exist.

(您可以在代码中看到此路径的中间部分是用户uid,所以我绝对有一个经过身份验证的用户)

(you can see in the code that the middle part of this path is the user uid, so I definitely have an authenticated user)

在测试"myimage.png"之类的值之前,我曾尝试简化此路径,但没有帮助,我遇到了同样的错误.

I tried to simplify this path before to test value like "myimage.png" but didn't help, I got the same error.

我已经尽力了,请帮忙,因为我在firebase文档中找不到任何相关内容.

I've already tried everything I could, please help me because I can't find anything related in the firebase documentation.

下面的代码在视图加载时自动打开图像选择器.如果用户选择图像,我们将其设置为图像视图.当用户单击上传"按钮时,控制器尝试将文件上传到Firebase存储.

The code below automatically opens the image picker when the view loads. If the user chooses an image, we set it to an image view. When the user clicks the upload button, the controller tries to upload the file to firebase storage.

import UIKit
import Firebase

class ShareViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!

    let imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self
        imagePicker.allowsEditing = false
        imagePicker.sourceType = .photoLibrary

        present(imagePicker, animated: true, completion: nil)
    }

    @IBAction func onPictureTapped(_ sender: Any) {
        present(imagePicker, animated: true, completion: nil)
    }

    @IBAction func onUploadClicked(_ sender: Any) {
        if let pickedImage = imageView.image, let imageData = UIImagePNGRepresentation(pickedImage) {
            let storageRef = Storage().reference()
            let imageRef = storageRef.child("images/\(Auth.auth().currentUser!.uid)/\(NSUUID().uuidString)")

            imageRef.putData(imageData, metadata: nil) { (metadata, error) in
                if error != nil {
                    NSLog(error!.localizedDescription)
                }

                self.navigationController?.popToRootViewController(animated: true)
            }
        }
    }

    internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            imageView.contentMode = .scaleAspectFit
            imageView.image = pickedImage
        }

        self.dismiss(animated: true, completion: nil)
    }
}

推荐答案

好,我发现了问题:

// Instead of this:
Storage().reference()

// You need to use this
Storage.storage().reference()

这很荒谬,没有关于此的警告,而且应用程序也不会崩溃.非常烦人,我花了至少5个小时才找到

This is ridiculous that there are no warnings about this, also the app don't crash. Very annoying, and it took me at least 5 hours to find

这篇关于iOS Firebase存储文件上传-对象不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆