gpt4 book ai didi

ios - 为什么我的图像没有缓存在 AlamofireImage 中?

转载 作者:可可西里 更新时间:2023-11-01 02:16:48 25 4
gpt4 key购买 nike

我现在一头雾水,我不知道为什么我每次尝试检索时都得到 nil。我正在传递正确的图像数据和标识符。这是一个小代码

 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

当我尝试检索缓存图像时打印它:URL 是什么? : http://www.somelink.com/DT199.jpg 我们实际返回的是:nil

我不知道为什么会这样,我之前试过几次,效果很好。我也读到了这些函数,它们不返回任何 bool 值响应,通过它我们可以知道图像是否在缓存中。

后来,我尝试使用下载管理器也没有任何反应,仍然毫 headless 绪。如果有人可以帮助我?这是代码

 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))")

}


}


})


}




}
}

最佳答案

也许您必须使用您之前使用的 photoCache 来初始化您的 imageDownloader 以手动缓存图像。 AutoPurgingImageCache 不是共享实例。只有 UIImageView+AlamofireImage.swift 扩展有一个 af_sharedImageDownloader,它保留一个 AutoPurgingImageCache。

根据您的代码示例,它应该如下所示:

let imageDownloader = ImageDownloader(
configuration: ImageDownloader.defaultURLSessionConfiguration(),
downloadPrioritization: .FIFO,
maximumActiveDownloads: 15,
imageCache: self.photoCache
)

关于ios - 为什么我的图像没有缓存在 AlamofireImage 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37536195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com