[英]iOS: Downloading the AWS S3 images & cache it for future use
我試圖從Amazon S3存儲桶下載圖像(webp圖像)並將其緩存,從早晨開始拔我的頭發。 無論采取什么方法,我都遇到了一些或其他路障。 我試圖實現這一目標的不同方式,
所以現在我可以選擇使用Amazon SDK下載圖像,然后使用SDWebImage的SDImageCache獨立緩存它,或者我需要將SDWebImage源代碼添加到我的項目中,然后我需要將其源代碼修改為采取授權標題。
請告訴我,如果有更好的方法可以實現這一目標,那么如果我能夠指出什么是實現我想要做的最佳方式,那就太棒了。
提前致謝。
說實話,我自己研究了很長時間,並且對AWS提供的內置緩存系統不滿意。 相反,我選擇使用Kingfisher https://github.com/onevcat/Kingfisher,因為它內置於swift並且使用得很好。 我強烈推薦它,因為它是高度可定制的,但也完美地“開箱即用”
您可以選擇Kingfisher用於緩存的關鍵字。 因此,創建一個預先簽名的s3 url,然后在加載圖像時使用s3鍵作為緩存而不是完整的預簽名URL。
let getPreSignedURLRequest = AWSS3GetPreSignedURLRequest()
getPreSignedURLRequest.bucket = media.bucket
getPreSignedURLRequest.key = media.key
getPreSignedURLRequest.httpMethod = .GET
getPreSignedURLRequest.expires = Date(timeIntervalSinceNow: 3600) // Change the value of the expires time interval as required
AWSS3PreSignedURLBuilder.default().getPreSignedURL(getPreSignedURLRequest).continueWith { (task:AWSTask<NSURL>) -> Any? in
if let error = task.error as NSError? {
print("Error: \(error)")
return nil
}
if let presignedURL = task.result {
DispatchQueue.main.async {
self.imageView.kf.indicatorType = .activity
let resource = ImageResource(downloadURL: URL(string: presignedURL.absoluteString!)!, cacheKey: media.key)
self.imageView.kf.setImage(with: resource)
}
}
return nil
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.