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

Custom image cache search paths #156

Closed
wants to merge 6 commits into from

Conversation

SRandazzo
Copy link
Contributor

This feature lets you define custom paths for SDWebImage to search for previously-cached-images.

This is particularly useful if you are bundling images with your app that have been cached by SDWebImage. (ie. if you are 'seeding' your app with a core-data file that contains a lot of URL's to images and would like to also seed those images without having to copy every one of them over)

For example, you can tell SDImageCache to add '[[NSBundle mainBundle] resourcePath]' as a custom path, so that the main bundle will be queried for cached images.

This prevents the need for you to copy pre-cached images over to the caches/ImageCache folder that SDImageCache normally checks for.

The custom paths are read-only.

…ths for images that have been previously cached with SDWebImage. This is particularly useful in cases where you may be bundling images that have been previously cached with SDWebImage. The need to copy those images over into the 'ImageCache' folder can be eliminated
@@ -221,6 +236,23 @@ - (void)queryDiskCacheOperation:(NSDictionary *)arguments

#pragma mark ImageCache

- (void)addCustomImageSearchCachePath:(NSString*)path
{
[userCachePaths addObject:path];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to consider guarding against the same path being added multiple times. While that's probably a user error, it's easy enough to be extra helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks.

Going to try and update this soon for the new 3.0 SDWebImage as this PR was based on the last version, will take your suggestion into account!

Adds public method for adding custom cache paths
Breaks apart default cache path and file name generation method
Add in support for the demo to demonstrate loading a gif from a path that is bundled with the app when built
@SRandazzo
Copy link
Contributor Author

@rs I've finally updated this for the latest SDWebImage!

I updated the demo as well to show how adding a custom path works, and bundled one of the images with the app (the gif to be specific)

@SRandazzo
Copy link
Contributor Author

@rs any chance this will get looked at/merged in?

We've been using it in production smoothly for a few weeks now, would love to get it official!

@rs
Copy link
Member

rs commented Jun 7, 2013

Merged

@rs rs closed this Jun 7, 2013
@timmolter
Copy link

@SRandazzo Could you help a newbie out? How do you go about seeding your app with images?

@SRandazzo
Copy link
Contributor Author

@timmolter Of course!

It's important that any images you want to "seed" are downloaded via SDWebImage (since SDWebImage manages generating the hash/file name based on the URL of the images)

To go about this, start with a fresh/clean cache (you can use the SDImageCache method, or just manually delete the files), then download all the images you need. For example, we sometimes just use an array of image URLs that we loop through and tell SDWebImage download. If your app is not user data specific, you can also just run through it, and any images downloaded will be in the cache folder (again, be careful of user data specific images)

It's not a bad idea to have a completely separate target that can run this code if you are going to do it often

Once all the images you need are downloaded, navigate in your Finder to here:
~/Library/Application Support/iPhone Simulator/7.0.3/Applications/d12312-123412-1234321/Library/Caches/com.hackemist.SDWebImageCache.default
Where d12312-123412-1234321 is your application
and 7.0.3 is the sdk you ran the app on when downloading all the files

Copy all the files inside of the com.hackemist.SDWebImageCache.default folder to a folder in your project folder and then to your project

I use something like "seed_image" for the folder name, then added that to the project (In this case, I typically add as a "folder reference")

from there, just use this code (in the PR/sample project)

 NSString *bundledPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"seed_image"];
[[SDImageCache sharedImageCache] addReadOnlyCachePath:bundledPath];

Obviously this could all stand to be automated, but we weren't swapping bundled images often enough to make it worth it

hope that helps

@timmolter
Copy link

@SRandazzo Wow! Thanks so much! I'll give it a shot.

@timmolter
Copy link

@SRandazzo God dang brilliant! Thanks for this!

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

Successfully merging this pull request may close these issues.

4 participants