我現在很無知,我不知道爲什麼每次我嘗試檢索時都會得到零。我傳遞正確的圖像數據和標識符。這裏有一個小代碼爲什麼我的圖像沒有被緩存在AlamofireImage中?
let photoCache = AutoPurgingImageCache(
memoryCapacity: 100 * 1024 * 1024,
preferredMemoryUsageAfterPurge: 60 * 1024 * 1024
)
func cacheImage(image: Image, urlString: String) {
print("Image size: \(image.size) and string as identifier: \(urlString)")
self.photoCache.addImage(image, withIdentifier: urlString)
}
func cachedImage(urlString: String) -> Image? {
print("What is url? : \(urlString)")
print("What we are actually returning? : \(self.photoCache.imageWithIdentifier(urlString))")
return self.photoCache.imageWithIdentifier(urlString)
}
打印爲cacheImage功能:圖像尺寸:(2826.0,3722.0)和字符串作爲標識符:HTTP:www.somelink.com/DT199.jpg
打印爲cachedImage時我正在嘗試檢索它:URL是什麼? :http:www.somelink.com/DT199.jpg我們實際返回的是:無
我不知道它爲什麼會發生我曾嘗試過幾次,它的工作完美。我還閱讀了這些函數,他們不返回任何布爾響應,通過它我們可以知道圖像是否在緩存中。
後來,我嘗試使用下載管理器以及沒有發生任何事情仍然無能爲力。如果有人能幫助我?這裏的代碼
let imageDownloader = ImageDownloader(
configuration: ImageDownloader.defaultURLSessionConfiguration(),
downloadPrioritization: .FIFO,
maximumActiveDownloads: 15,
imageCache: AutoPurgingImageCache()
)
let downloader = imageDownloader
Alamofire.request(.GET, request, headers: headers)
.responseJSON { response in
if let JSON = response.result.value {
self.imagesArray = NSMutableArray()
self.imagesArray = JSON["results"] as! NSMutableArray
for results in self.imagesArray{
let URLRequest = NSURLRequest(URL: NSURL(string:results["url"] as! String)!)
downloader.downloadImage(URLRequest: URLRequest, filter: nil, completion: { response in
if response.response?.statusCode == 200{
print("In the house fetching url: \(results["url"] as! String)")
let image = response.result.value
print("Size of image: \(image?.size)")
let returnValue = downloader.imageCache?.addImage(image!, withIdentifier: results["url"] as! String)
print("return status: \(returnValue)")
if results .isEqual(self.imagesArray.lastObject){
self.activityIndicatorView.stopAnimating()
self.activityIndicatorView.hidden = true
let fic = self.imagesArray.firstObject
print("getting image for this url : \(fic!["url"] as! String)")
var imageC = UIImage()
imageC = (downloader.imageCache?.imageWithIdentifier(fic!["url"] as! String))!
self.ssImage.image = imageC
print("Image : \(imageC.size))")
}
}
})
}
}
}