AlamofireImage保存到缓存的图像并未真正保存

编程入门 行业动态 更新时间:2024-10-22 14:35:25
本文介绍了AlamofireImage保存到缓存的图像并未真正保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用(尝试)AlamofireImage。 我保存到缓存的图像并未真正保存。

I'm using (trying) AlamofireImage. My saved-to-cache images are not really saved.

我的代码包括:

import Alamofire import AlamofireImage let imageCache = AutoPurgingImageCache( memoryCapacity: 100 * 1024 * 1024, preferredMemoryUsageAfterPurge: 60 * 1024 * 1024 )

然后我自己使用以下方式保存图像:

then i am saving an image myself using:

imageCache.add(image, withIdentifier: path)

,但是在下一次运行中,当我尝试使用相同的路径获取此图像时,它始终返回nil:

but in the next run, it always returns nil when i try to fetch this image using the same path:

if let image = imageCache.image(withIdentifier: path){ return image } return nil

我在做什么错了?

  • 在运行\调试时我可以看到表示imageCache-currentMemoryUsage = 0

谢谢。

推荐答案

您不需要调用add()方法。我创建了包装Alamofire图片缓存的自定义类。

You don't need to call add() method. I create my custom class which wrap Alamofire Image cache.

class ImageManager: NSObject { static let shared = ImageManager() private var imageDownloader: ImageDownloader! private var imageCache: AutoPurgingImageCache! // Configuration var maximumActiveDownloads = 5 var placeholderImage: UIImage! var imageTransitionDuration: NSTimeInterval = 0.5 // The total memory capacity of the cache in MB. var memoryCapacity: UInt64 = 150 //The preferred memory usage after purge in MB. During a purge, images will be purged until the memory capacity drops below this limit. var memoryUsageAfterPurge: UInt64 = 60 private override init() { super.init() imageCache = AutoPurgingImageCache( memoryCapacity: memoryCapacity * 1024 * 1024, preferredMemoryUsageAfterPurge: memoryUsageAfterPurge * 1024 * 1024 ) imageDownloader = ImageDownloader( configuration: ImageDownloader.defaultURLSessionConfiguration(), downloadPrioritization: .FIFO, maximumActiveDownloads: maximumActiveDownloads, imageCache: imageCache ) UIImageView.af_sharedImageDownloader = imageDownloader } func setImageOrPlaceholder(onImageView imageView: UIImageView, stringURL: String?) { guard let correctURL = NSURL.getURL(fromString: stringURL) else { //debug("URL incorrect!: \(stringURL)", isError: true) imageView.image = placeholderImage return } let imageTransition: UIImageView.ImageTransition = .CrossDissolve(imageTransitionDuration) imageView.af_setImageWithURL(correctURL, placeholderImage: placeholderImage, filter: nil, imageTransition: imageTransition, completion: { response in // debug("\(response.result.value)") }) } }

更多推荐

AlamofireImage保存到缓存的图像并未真正保存

本文发布于:2023-11-26 02:59:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   图像   AlamofireImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!