将图像上传到Firebase存储

时间:2020-03-10 04:58:36

标签: ios swift firebase jpeg firebase-storage

我一直在使用Firebase指南将用户图像上传到Firebase存储,但是在上传用户图像后,文件夹中没有任何内容。如何解决此问题,以成功将图像上传到Firebase存储中,我还缺少什么?

大小0字节

let storage = Storage.storage()
        let storageRef = storage.reference()
         let image = UIImage(named: "ProfileImage")
       let data = Data()
         let starsRef = storageRef.child("ProfileImage.jpeg")
        let metadata = StorageMetadata()
        metadata.contentType = "ProfileImage/jpeg"
        let uploadTask = starsRef.putData(data, metadata: nil) { (metadata, error) in
         guard let metadata = metadata else {
           return
         }

         let size = metadata.size

         starsRef.downloadURL { (url, error) in
           guard let downloadURL = url else {

             return
           }
         }
       }

3 个答案:

答案 0 :(得分:1)

关于将图像上传到Firebase存储并检索URL路径以供以后使用的步骤,这里有6个步骤。

  1. 使用UUID().uuidString
  2. 创建一个唯一的名称
  3. 将图像压缩到compressionQuality
  4. 将metaData设置为.jpeg
  5. 将数据添加到Firebase存储
  6. 如果成功,则获取图像URL
  7. 将URL转换为url?.absoluteString并使用print

    打印出来
        //1. Create an unique name for your image
        let imageName = UUID().uuidString
        let imageReference = Storage.storage().reference().child(imageName)
    
        //2. Compress quality
        if let uploadData = self.tempImageView.image!.jpegData(compressionQuality: 0.5){
    
            //3. Save image as .jpeg
            let metaDataForImage = StorageMetadata()
            metaDataForImage.contentType = "image/jpeg"
    
            //4. Add the data to Firebase Storage
            imageReference.putData(uploadData, metadata: metaDataForImage) { (meta, err) in
                if let err = err{
                    print(err.localizedDescription)
                }
                else{
                    //5. Retrieving the image URL
                    imageReference.downloadURL { (url, err) in
                        if let err = err{
                            print(err.localizedDescription)
                        }
                        else{
                            //6. Print the complete URL as string
                            let urlString = url?.absoluteString
                            print(urlString)
                        }
                    }
                }
            }
        }
    

答案 1 :(得分:0)

let data设为guard let data,这样一来,当您需要非可选选项时,就不必强行拆包。我说一般来说通常避免用力展开。

答案 2 :(得分:0)

  1. contentType应该是image/jpeg。我googled some piece codes,它显示了相同的概念。
  2. 关于contentTypemore detail at this wiki link
  3. 如果您上载jpeg格式的图像,则应该上载jpeg二进制文件。但是data = image.pngData()似乎是png的二进制数据。

// data is `png` binary
let data = image.pngData()
// imageData is `jpeg` binary 
let imageData = image.jpegData(compressionQuality: 0.9)
let uiImage: UIImage = UIImage(data: imageData!)!

 let starsRef = storageRef.child("ProfileImage.jpg")
let metadata = StorageMetadata()

// About contentType
// See it: https://en.wikipedia.org/?title=Content_type&redirect=no 
metadata.contentType = "ProfileImage/jpeg"

// data is `png` binary, but contentType is `jpeg` ? 
let uploadTask = starsRef.putData(data!, metadata: nil) { (metadata, error) in
 guard let metadata = metadata else {
   return
 }