Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to prefetch images the first time? #83

Open
houmie opened this issue Jul 4, 2015 · 6 comments
Open

How to prefetch images the first time? #83

houmie opened this issue Jul 4, 2015 · 6 comments

Comments

@houmie
Copy link

houmie commented Jul 4, 2015

SDWebImage does already something like that:

[[SDWebImagePrefetcher sharedImagePrefetcher] prefetchURLs:<NArray with image URLs>];

Is there something like this in Haneke?

@DiAvisoo
Copy link

DiAvisoo commented Jul 4, 2015

Not sure if this is the best way to go about this, but I solved it by creating a preload method that basically just loads the image to a dummy UIImageView. Works like a charm.

( I believe you need to use the same HNKCacheFormat when you load it for real. )

+ (void)preloadImage:(NSString *)imagePath
{
    NSLog(@"Pre-loading image %@", imagePath.lastPathComponent);

    UIImageView *imageView = [[UIImageView alloc] init];
    HNKCacheFormat *format = [HNKCache sharedCache].formats[@"cacheImageFormat"];
    if (!format)
    {
        format = [[HNKCacheFormat alloc] initWithName:@"cacheImageFormat"];
        format.size = CGSizeMake(1024, 768);
        format.scaleMode = HNKScaleModeNone;
        format.compressionQuality = 1;
        format.diskCapacity = 10000 * 1024 * 1024;
        format.preloadPolicy = HNKPreloadPolicyAll;
    }
    [imageView setHnk_cacheFormat:format];

    if([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
        [imageView hnk_setImageFromFile:imagePath];
    }
}

@houmie
Copy link
Author

houmie commented Jul 4, 2015

Thank you for your reply. Your post was very inspiring and I did exactly what you suggested.
Unfortunately it doesnt work very reliable. If I disconnect Internet, hoping that those images should be cached and retrieved without internet connection, some of them don't show up and show the placeholder image instead.

@DiAvisoo
Copy link

DiAvisoo commented Jul 4, 2015

I used this way to make sure a rather big local image would get loaded on the next UIViewController immediately rather than have a slight (but annoying) delay once the UIViewController appeared. Didn't try it for remote images though, too bad it didn't work :(

@houmie
Copy link
Author

houmie commented Jul 4, 2015

I take it back. It works!!!

The mistake I made was, setting the cache too small.

format.diskCapacity = 1 * 1024 * 1024;

It simply ran out of cache space, hence it acted unreliably. I think I need to play with it and find a sensible number.

Thank you so much for your help!!

@DiAvisoo
Copy link

DiAvisoo commented Jul 5, 2015

Alright! Glad I could help :)

@cameronehrlich
Copy link

Is this really the best/proper way to do this?
I feel like there should be a more elegant solution...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants