moa icon indicating copy to clipboard operation
moa copied to clipboard

determining if image was loaded from cache

Open rjuzaszek opened this issue 7 years ago • 5 comments

Hello, great work, really appreciate simplicity of this lib, but I found myself in a situation when I would like to determine if image was really downloaded or loaded from a cache - for example to show or not to show an animation, is there a simple way to determine that (maybe using duration field in MoaImage)?

desired example:

profileImage.moa.onSuccessAsync = {  image in
    if !image.loadedFromCache {
        UIView.animate(withDuration: 0.3, animations: {
            self.profileImage.layer.opacity = 1.0
        })
    }
   return image
}

rjuzaszek avatar Jun 14 '17 08:06 rjuzaszek

Hi @rjuzaszek, good question. I don't know the answer, unfortunately. The library uses the built-in URLSession class for downloading the image and caching. I did a quick search and could not find a way to tell if data was taken from cache.

Do you need to display a "loading..." spinner animation?

evgenyneu avatar Jun 14 '17 09:06 evgenyneu

Thanks for reply. Yes I'm displaying it, but the problem is to not to display the animation if image is taken from cache which causes the image blink instantly after showing the VC, which I would like to avoid, if I wouldn't find any simple way I think I'll just skip opacity animation in every situation.

rjuzaszek avatar Jun 14 '17 09:06 rjuzaszek

Oh, I see now, thanks for clarification. So your goal is to show the image instantly if it's taken from cache or fade it in if it's downloaded from the network?

evgenyneu avatar Jun 14 '17 09:06 evgenyneu

Yes

rjuzaszek avatar Jun 14 '17 09:06 rjuzaszek

Interesting ... I can't think of any good solution at the moment. My first thought was to check the duration of time elapsed between assigning the URL and calling onSuccess. If it's less than N milliseconds, then it could be from cache. But it does not look like a robust solution to me. A more reliable approach would be to either always show image with or without animation. I am sorry, I could not help you. :(

evgenyneu avatar Jun 14 '17 10:06 evgenyneu