AlamofireImage saved-to-cache images are not really saved

佐手、 提交于 2019-12-25 07:59:56

问题


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

my code include:

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)

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

what am i doing wrong?

  • while running\debugging i can see that the imageCache-currentMemoryUsage = 0

Thanks.


回答1:


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



回答2:


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

This sounds like you're restarting your app. The ImageCache is memory only, and your app's memory is reclaimed when you restart the app.



来源:https://stackoverflow.com/questions/39828215/alamofireimage-saved-to-cache-images-are-not-really-saved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!