我需要AlamofireImages一點幫助。在應用中,輕敲表視圖細胞會下載並設置使用內置的ImageView的擴展,下面看到的圖像:緩存AlamofireImage響應磁盤
func sendImageRequest(imageView: UIImageView, item: CatalogItem, isLargeImage: Bool? = false){
let imageURL = ImageManager.URLBuilder(sku: item.sku, largeImage: isLargeImage)
// Clear the image so that recycled images are cleared.
imageView.image = nil
// Create the request and add the token string to the authorization header
var urlRequest = try! URLRequest(url: URL(string: imageURL)!)
urlRequest.setValue("Bearer \(ImageManager.getTokenString()!)", forHTTPHeaderField: "Authorization")
// If the token exists AND is VALID (not expired) - Get the image.
if ImageManager.checkIfTokenExists() && ImageManager.checkTokenIsValid(){
debugPrint("Valid token already exists. Fetching image... TableViewExtension")
imageView.af_setImage(withURLRequest: urlRequest){ response in
switch response.result{
case .success(let image):
debugPrint(response)
debugPrint("SUCCESS")
break
case .failure(let error):
debugPrint("Error: \(error)")
debugPrint(response.response)
break
}
}
}
// Else if the token exists AND is INVALID (expired) - Delete the old token, get a new one, and fetch the image
else if ImageManager.checkIfTokenExists() && !ImageManager.checkTokenIsValid(){
debugPrint("Token expired... Getting new token.")
ImageManager.deleteToken()
let tokenRequest = NetRequest.newTokenRequest(url: "http://\(SettingsManager.KEY_ServerURL).ziizii.io/zz/jwt/new?service=images.ziizii.io")
tokenRequest.requestJWTToken(){
debugPrint("Token renewed. Fetching image...")
imageView.af_setImage(withURLRequest: urlRequest)
}
}
// If the token doesn't exist, request a new one and fetch the image.
else{
debugPrint("Requesting new token...")
let tokenRequest = NetRequest.newTokenRequest(url: "http://\(SettingsManager.KEY_ServerURL).ziizii.io/zz/jwt/new?service=images.ziizii.io")
tokenRequest.requestJWTToken(){
debugPrint("Token aquired. Fetching image...")
imageView.af_setImage(withURLRequest: urlRequest)
}
}
}
所以,這部分我沒事用。它的工作原理是,將對象緩存到磁盤並設置ImageView圖像。如果稍後再次點擊它,它會檢查圖像是否在緩存中,如果是,只需應用圖像。整齊。
現在,項目的一個新要求是讓用戶能夠預先載入每個圖像並緩存它們。現在我不再使用AFI ImageView擴展,而是使用Downloader。我不確定如何讓Downloader到: A)緩存到磁盤和 B)當用戶點擊一個單元格時,讓該ImageView擴展程序檢查相同的緩存以查看它是否預加載並應用該圖像。
AFI在發送請求之前足夠聰明地檢查緩存中的圖像數據,所以我確信這是可能的。
我至今下載下面的圖片是:
static var downloader = ImageDownloader.default
static func downloadImage(urlRequest: URLRequestConvertible)
{
self.downloader.download(urlRequest) { response in
switch response.result{
case .success(let image): // If successful, cache the image.
print("SUCCESS")
break
case .failure(let error): // If unsuccessful, remove the cached 404 response and apply a nil image.
print("ERROR: \(response.error)")
break
}
}
}
如果任何人有任何想法如何做到這一點或任何有用的提示或技巧,這將是偉大的!謝謝!