evgenyneu / moa

An image download extension of the image view written in Swift for iOS, tvOS and macOS.
MIT License
332 stars 45 forks source link

determining if image was loaded from cache #15

Open rjuzaszek opened 7 years ago

rjuzaszek commented 7 years ago

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
}
evgenyneu commented 7 years ago

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?

rjuzaszek commented 7 years ago

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.

evgenyneu commented 7 years ago

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?

rjuzaszek commented 7 years ago

Yes

evgenyneu commented 7 years ago

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. :(