AlamofireImage saved-to-cache images are not reall

2019-09-04 05:45发布

问题:

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.