0

我已经能够成功地将图像上传到 firebase 存储,但无法成功下载图像。

我已尝试以 Firebase 存储指南上建议的所有三种方式下载图像:

1) 下载到内存中的 NSData 2) 下载到代表设备上文件的 NSURL 3) 在线生成代表文件的 NSURL

下面是两个不同尝试的示例:

func loadProfileImage() {
        guard let currentUser = Auth.auth().currentUser else { return }
        let profilePhotoFile = "ProfileImages/" + currentUser.uid
        let reference = Storage.storage().reference(withPath: profilePhotoFile)

#1st 尝试下载到内存:

    reference.getData(maxSize: 1 * 1024 * 1024) { (data, error) in
        if let error = error {
            print("an error occurred: \(error)")
            print("see data response: \(data)")
        }  else {
            self.profileView.image = UIImage(data: data!)
        }
     }

#2nd尝试下载网址:

     reference.downloadURL { (url, error) in
        if let error = error {
            print(error)
        } else {
            self.profileView.sd_setImage(with: url, placeholderImage: 
           #imageLiteral(resourceName: "placeHolderProfileView")) { 
         (image, error, type, reference2) in
                print("reference location of image in the google 
     bucket: \(reference2)")
                print("error retrieving image: \(String(describing: 
            error))")
                print("type: \(type)")
                print("image details: \(String(describing: image))")
            }

        }

    }
}

还尝试使用 alamofire 而不是 SDWebImage 来查看错误代码是否相同,如下所示:

" Error Domain=FIRStorageErrorDomain Code=-13000 "发生未知错误,请检查服务器响应。" UserInfo={object=ProfileImages/6I2RhzFI3edYNph9J4WsaXXXX, ResponseErrorCode=100, bucket=bXXXX-production.appspot.com, NSLocalizedDescription=发生未知错误,请检查服务器响应。, ResponseErrorDomain=NSPOSIXErrorDomain, _kCFStreamErrorDomainKey=1, NSErrorPeerAddressKey={length = 28,容量 = 28,字节 = 0x1c1e01bb000000002607f8b040090813 ... 0000200a00000000},_kCFStreamErrorCodeKey=100}

我已经检查并重新检查了谷歌存储桶的位置,并相信我的参考位置是正确的(使用与正常工作的上传文件路径相同)。

任何帮助将非常感激

4

2 回答 2

0

你去:

func downloadImage(url : String,
                       completionHandler: @escaping (Bool?, UIImage?, String?) -> Void) -> Void
    {
        var success : Bool = false
        var img : UIImage? = nil
        var errorLog : String? = nil

        let u = URL(string: url)

        let task = URLSession.shared.dataTask(with: u!, completionHandler: { (data, response, error) in

            if error != nil
            {
                errorLog = error?.localizedDescription
                completionHandler(success, img, errorLog)
            }
            else
            {
                success = true
                img = UIImage(data: data!)
                completionHandler(usuccess, img, errorLog)
            }

        })

        task.resume()

    }

使用以下方式获取 URL:

imgReference.downloadURL { (url, error) in
   guard let url = url else { return } 
   urlString = url.absoluteString
   //do something with the urlString (such as download image)
}
于 2018-05-22T15:39:37.857 回答
0

意识到错误出现在上传图片时包含的标题中:

我最初在上传时列出了以下内容,通过将它们注释掉,我能够使用 SDWebImage 和 vbuzze 的建议成功下载。

让 uploadMetadata = StorageMetadata() uploadMetadata.contentType = "image/jpeg" uploadMetadata.customMetadata = ["Profile Name" : currentUser.displayName] as? [字符串:字符串]

于 2018-05-22T16:07:01.913 回答