Reputation:
I am using SDWebImage to download images from different URL's, now I was wondering if it is possible to download these images store them some where so when I am offline and launch my app I can still see them?
I noticed Instagram uses a similar thing, once you have downloaded the images you can close the app go offline and then reopen the app and you can still see the images.
I think this is possible with SDWebImage but not sure, please do correct me if it is not possible.
Many thanks in advance to anyone that can help!
Upvotes: 2
Views: 2145
Reputation: 813
You can store your image in app directory using SDWebImage
instead of store manually. It'll be more simple while you gonna use SDImageCache
. diskImageExistsWithKey
determines you whether your image is exists in location (in directory or in you tempfolder of app) with a callback. You just have check it! ...
[[SDImageCache sharedImageCache] diskImageExistsWithKey:localImagePath completion:^(BOOL isInCache) {
if ( isInCache ) {
image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:localPath]; // it returns a simple UIImage
[yourImageView setImage:[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:localPath]]; // or set image in your UIImageView
} else {
[yourImageView sd_setImageWithURL:[NSURL URLWithString:downloadPath] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
if (image) {
[[SDImageCache sharedImageCache] storeImage:image forKey:localPath toDisk:YES completion:^{ // This block will help you to store image from server to local directiry
NSLog(@"Downloaded");
}];
}
}];
}
}] ;
Upvotes: 1
Reputation: 17132
I assume what you want to archive is that kind of feed, where you have like 4-5 pictures shown after your App has been killed/restarted and you don't have an internet connection - like Instagram does.
A possibility for you would be, to download images as you're used to when you have an internet connection and store like 4-5 images into your core data as NSData.
Then you are able to use those Images as placeholders and you would have "content" while the user has no connection.
Here is code to convert an image to NSData and back:
let dataFromImage = UIImagePNGRepresentation(image!)!
let imageFromData = UIImage(data: imageData)
And here is a perfect tutorial of how to store images into core data.
Then you are able to populate the tableView (for example) with images from your core data in case reachability == false
.
Upvotes: 1
Reputation: 3831
import the UIImageView+WebCache.h
header, and call the setImageWithURL:placeholderImage: method in your viewcontroller. SDWebImage lib will be handled for you, from async downloads to caching management.
Upvotes: 1