AlamofireImage保存到缓存的图像并未真正保存 [英] AlamofireImage saved-to-cache images are not really saved

查看:260
本文介绍了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保存到缓存的图像并未真正保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆