hyperoslo / Lightbox

:milky_way: A convenient and easy to use image viewer for your iOS app
https://www.hyper.no
Other
1.63k stars 329 forks source link

Add ability to cancel image preloading #217

Closed JonasGessner closed 4 years ago

JonasGessner commented 5 years ago

Preloading operations currently cannot be cancelled, which may result in a number of redundant background operations fetching images that are no longer needed. The default implementation uses a complicated workaround using associated objects to be able to cancel previous fetch operations (I believe this specific code is part of Imaginary). To allow custom image providers (via LightboxConfig.loadImage) to be able to cancel operations when the preloading operation is no longer needed, I added a cancel operation to LightboxConfig.loadImage. This closure may be provided by the implementation of LightboxConfig.loadImage and will be called when the specific requested image no longer needs to be prefetched (i.e. the associated image view receives a new image to fetch).

Moreover, a non-cancelled image prefetch operation will end up setting the image of an image view that no longer requested that image, which results in displaying incorrect images.