Haneke / HanekeSwift

A lightweight generic cache for iOS written in Swift with extra love for images.
Apache License 2.0
5.2k stars 591 forks source link

[Bug] Reuse same image with different format, custom fetcher not working as expected ? #389

Closed tirrorex closed 7 years ago

tirrorex commented 7 years ago

Hi, I have an app where i use the same image multiple times but with totally different format. I currently have implemented a custom fetcher which should be working, setting the image after download with proper format but it isn't, code below

The format i have for parameter is applying a resize. I tested this with a custom controller, i display the image as a thumbnail first and i have a detail view which is displaying a bigger image, but the detail view is unfortunatly displaying the thumbnail. Both images have the same url but format and resize methods are different.

 `    private static func fetchImageForDifferentFormat(imageView: UIImageView, imageName: String, format: Format<UIImage>) {
    let cache = Shared.imageCache
    // trying to fetch the image with specific format

    cache.addFormat(format)
    cache.fetch(key: imageName, formatName: format.name, failure: { error in
        // image does not exist, trying to fetch the original image from memory
        cache.fetch(key: imageName, formatName: HanekeGlobals.Cache.OriginalFormatName, failure: { error in
            // image does not exist, need to fetch from URL
            cache.fetch(URL: NSURL(string: imageName)!, formatName: HanekeGlobals.Cache.OriginalFormatName, failure: { (error) in }, success: { image in

                // set image to cache with original format for ulterior use
                cache.set(value: image, key: imageName, formatName: HanekeGlobals.Cache.OriginalFormatName, success: { (image) in
                })
                // set redimensionned image to imageView and store it in cache
                cache.set(value: image, key: imageName, formatName: format.name, success: { (image) in
                    imageView.image = image
                })
            })}, success:  { image in
                // image exists in memory with original format, need to be set at proper format and cached

                cache.set(value: image, key: imageName, formatName: format.name, success: { (image) in
                    imageView.image = image})

        })} , success:  { image in
            // image exists with proper size in memory
  // breakpoint succeed
            imageView.image = image
    })
}`

I put some breakpoints in the code but the behaviour i have makes no sense, the breakpoint i called "breakpoint succeed" is called multiple times for the same image, first when the method is called (the image is not there yet with this format) and then at the end of EVERY error callback. I don't get it

tirrorex commented 7 years ago

Managed to fix it, was an upscaling problem