图片上传到Firebase存储空间后,图片大小会增加。

问题描述 投票:0回答:1

我上传了一张7.4 MB大小的图片到firebase存储空间,但我得到了24.8 MB。为什么会出现这种情况?我该如何解决呢?

注意:我没有对图片的大小进行任何修改。我没有改变图片的大小,我保持了它的原样。

这里是代码。

    func storeImageInFirebase(){
           let storeageRef = Storage.storage().reference()  
           let imageName = UUID().uuidString + ".jpg"
           let imagesReference = storeageRef.child("images").child(imageName)
           let imageData = self.imgView.image!.pngData()
           let metaData = StorageMetadata()


           metaData.contentType = "image/jpg"
           imagesReference.putData(imageData!, metadata: metaData){ (metadate, error)
               in
               guard metadate != nil else{
                   print("Error: \(String(describing: error?.localizedDescription))")
                   return
               }
              // Fetch the download URL
               imagesReference.downloadURL(completion: {(url, error)
                   in
                   if error != nil {
                       print("Faild to download url:", error!)
                       return
                   }else{
                       // show the url in real database


                   }
               })

           }

       }
swift firebase firebase-storage
1个回答
1
投票

试试下面的代码。你可以改变maxCompression和maxFileSize来满足你的需求。这将继续循环压缩文件,当图像大于你的最大尺寸和压缩高于你的最大压缩时,以0.05为增量。

func storeImageInFirebase(image: UIImage) {
    var compression: CGFloat = 0.9 //starting compression
    let maxCompression: CGFloat = 0.05 //change to the maximum compression you want
    let maxFileSize: Int = 512 * 512 //change to the maximum file size you want
    guard var uploadImageData = image.jpegData(compressionQuality: compression) else {
        print("ERROR: Creating photo data")
        return
    }

    while (uploadImageData.count > maxFileSize) && (compression > maxCompression) {
        compression -= 0.05
        if let compressedImageData = image.jpegData(compressionQuality: compression) {
            uploadImageData = compressedImageData
        }
    }

    guard let uploadImageDataFinal = image.jpegData(compressionQuality: compression) else {
        print("ERROR: Compressing final photo")
        return
    }

    let imageName = UUID().uuidString
    let storageRef = Storage.storage().reference().child("images").child(imageName)

    let metadata = StorageMetadata()
    metadata.contentType = "image/jpeg"
    storageRef.putData(uploadImageDataFinal, metadata: metadata) { (metadata, err) in
        if err != nil {
            print("ERROR: Adding photo to storage")
            return
        } else {
            //success
            print("SUCCESS: Photo uploaded")
            return
        }
    }
}

要下载图片,请将此功能添加到视图控制器中。

func getImage(handler: @escaping(_ image: UIImage?) -> ()) {
    let imageName = UUID().uuidString
    let storageRef = Storage.storage().reference().child("images").child(imageName)
    storageRef.getData(maxSize: 27 * 1024 * 1024) { (data, error) in
        if let data = data, let image = UIImage(data: data) {
            print("SUCCESS: downloaded image")
            handler(image)
        } else {
            print("ERROR downloading image. \(error)")
            handler(nil)
        }
    }
}

当你需要下载图片时,使用:

    self.getImage { (returnedImage) in
        if let image = returnedImage {
            //use image here
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.