简体   繁体   English

将图像上传到 Firebase 存储

[英]Uploading a image to firebase storage

I have been using the firebase guides to upload a user image to firebase storage but after I upload the user image nothing appears in the folder.我一直在使用 firebase 指南将用户图像上传到 firebase 存储,但在我上传用户图像后,文件夹中没有任何内容。 How do I solve this problem to successfully upload my image to firebase storage is there anything i'm missing ?我如何解决这个问题以成功将我的图像上传到 firebase 存储有什么我遗漏的吗?

Size 0 bytes大小 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
           }
         }
       }

Here is 6 steps on how to upload the image to Firebase Storage and retrieve the URL path for later usage.以下是关于如何将图像上传到 Firebase 存储并检索 URL 路径以供以后使用的 6 个步骤。

  1. Create an unique name using UUID().uuidString使用UUID().uuidString创建唯一名称
  2. Compress the image into compressionQuality将图像compressionQualitycompressionQuality
  3. Sat the metaData as .jpeg将元数据设为 .jpeg
  4. Add the data to Firebase Storage将数据添加到 Firebase 存储
  5. If succeeded, retrieve the image URL如果成功,检索图像 URL
  6. Convert the URL to url?.absoluteString and print it out using print将 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) } } } } }

Make that let data a guard let data , that way you won't have to force-unwrap when you need it to be non-optional.使let data成为guard let data ,这样当您需要它是非可选的时,您就不必强制解包。 I'd say generally avoid force-unwrapping in general.我会说一般避免强制展开。

  1. The contentType should be image/jpeg . contentType应该是image/jpeg I googled some piece codes , it shows the same concept.我用谷歌搜索了一些代码,它显示了相同的概念。
  2. About contentType , more detail at this wiki link .关于contentType在这个 wiki 链接上有更多详细信息
  3. If you upload an image with jpeg format, then you should upload jpeg binary.如果您上传 jpeg 格式的图像,那么您应该上传jpeg二进制文件。 But data = image.pngData() , it seems to be the binary data of png .但是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
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM