swift - 图片不使用 Firebase url 缓存,但使用其他缓存

标签 swift caching firebase

我没有什么问题。我使用以下 URL 进行缓存:

let URL = NSURL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/kingfisher-\(indexPath.row + 1).jpg")!

但无法使用此 URL 让它像这样工作:

FIRStorage.storage().reference().child("\(productImageref!).png").downloadURLWithCompletion({(url, error)in

            if error != nil{
                print(error)
                return
            }else{

                cell.snusProductImageView.kf_setImageWithURL(url , placeholderImage: nil,
                    optionsInfo: [.Transition(ImageTransition.Fade(1))],
                    progressBlock: { receivedSize, totalSize in
                        print("\(indexPath.row + 1): \(receivedSize)/\(totalSize)")
                    },
                    completionHandler: { image, error, cacheType, imageURL in
                        print("\(indexPath.row + 1): Finished")

                })
            }
        })

我在这里做错了什么?你能给我指出正确的方向吗?对于缓存,我使用第 3 方库“KingFisher

编辑:Firebase 人 Mike McDonald 的引述

"The Github one has Cache-Control: max-age=300 while Firebase Storage doesn't have cache control set by default (you can set it when you upload the file, or change it by updating metadata), so I assume that's why KingFisher isn't caching it."

KingFisher 所有者 quotes .

最佳答案

就像 Firebase 人 Mike McDonald 说的那样,我在将图像上传到 Firebase 存储时直接设置了缓存控制。然后在后面加载的时候会正确缓存。

(配置:Swift3,Firebase Storage 3.11.0)

let storage = FIRStorage.storage()

let storageRef = storage.reference(forURL: yourFirebaseStorageReferenceURL)

let imageRef = storageRef.child("profileImages/\(imageUUID).jpg")

let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"

// THIS is the important part
metadata.cacheControl = "public,max-age=2592000"

let uploadTask = imageRef.put(imageData, metadata: metadata) { (metadata, error) in
   if let error = error
   {
        completion(.failure(error as NSError))
   }

   guard let metadata = metadata, let downloadURL = metadata.downloadURL()?.absoluteString else {
        // Handle upload error
        return
   }

   // Handle successful upload
}

当我下载图片时,设置了缓存控制

enter image description here

关于swift - 图片不使用 Firebase url 缓存,但使用其他缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38979706/

相关文章:

javascript - html5离线缓存表单数据

php - 消除 Excel 文档的缓存

firebase - 有没有什么方法可以设置源自多个 firestore 集合的 firestore 触发器以仅触发一个云(javascript)函数?

java - 如何将文件/位图上传到 Cloud Firestore?

ios - 如何在通过导航堆栈推送时为 View Controller 交叉溶解设置动画?

iOS Swift - CGSizeFromString 抛出 CGSizeZero

swift - 如何让 Sprite 在点击屏幕时左右移动?

c# - Entity Framework 6 编译的 LINQ 查询

ios - 文件不会写入磁盘。我究竟做错了什么?

javascript - 在 JavaScript 中保护 Firebase Secret