kean/Nuke

First render of cached images is delayed a few UI cycles

adamvnovak opened this issue · 1 comments

Here is a link to Google Drive videos describing the issue that occurs in the Nuke Demo. When the images are first rendered, there is a slight delay.

This doesn't look too bad for some apps, but it is unfortunately unacceptable for my use case. I need cached images to render immediately when the UIImageView is loaded, without any kind of temporary "loading" animation or image.

I think this might be related to this issue here posted on Stack Overflow. I implemented a similar custom solution as the accepted answer: creating a separate UIGraphicsBeginImageContext for the image beforehand.

Is there anyway to prepare images so that they render immediately upon the first draw of UIImageView using Nuke? Somehow preload them from the cache so they can be loaded in asynchronously? Or is there any workaround or roadmap for this to be added? Is this related to issue 716 here? If not I will have to use another solution for now.

kean commented

Like most other frameworks, Nuke loads cached images from disk asynchronously, and you can see the result on the videos you shared. The framework doesn't know how big the images are, so it always default to loading them asynchronously to avoid doing any work on the main thread.

If you know the images are small, you can roll out a custom image view and load them synchronously from the disk cache. You can use a convenience ImagePipeline.Cache/cachedImage() API. Please note that It only works if you have a pipeline with an aggressive cache enabled.

There are probably other ways to achieve it. I haven't tried it, but maybe you could also leverage ImagePrefetcher. In viewDidLoad, start prefetching the images for the first N cells and set the didComplete closure`. Use a semaphore to block the thread until the prefetching completes. Make sure also to set a timeout to avoid blocking it for too long: ~200-300 ms should do. The expected result would be that the images will already be available in the memory cache by the time the collection view re-draws. And, of course, it's always recommended to use the prefetching as the user scrolls the collection view.

Is this related to issue #716 here?

No

another solution

If you find anything that solves it out of the box, I would appreciate if you could post it here.