Different cache types SDWebImage - ios

I'm using SDWebImage for a while caching all my images but now i want to have more than one cache to to group various types of images. For example three kind of caches with several images each, so in runtime i want to clear one of them or have different setMaxCacheAge
Example:
types images = car images is one type, motorcycle is another... airplanes other.. like this. After i store this images i want delete or clear cache only of the motorcycle images (one type)
Now I have this but is for every images cached:
SDImageCache * sDImageCache = [SDImageCache sharedImageCache];
[sDImageCache setMaxCacheAge:60*60*24];
...
SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache clearMemory];
[imageCache clearDisk];
[imageCache cleanDisk];
-
I saw this but is really that i want?
SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:#"myNamespace"];
Using Asynchronous Image Caching Independently

Ok I figure out how to do...and I will share
First i removed all evidences of [SDWebImageManager sharedManager], because I want to do everything manually.
Then [SDWebImageDownloader sharedDownloader] to request new one and queryDiskCacheForKey (SDImageCache) to get local image (disk or memory)
How:
Create new imageCache with specific namespace
self.imageCache = [[SDImageCache alloc] initWithNamespace:#"nameSpaceImageCacheXPTO"];
[_imageCache setMaxCacheAge:oneHour * 3]; // 3 hours
Check if the image (key) is already in cache if not will request new one and save later
/////////
// * Prepar Key
/////////
NSString * key = url.absoluteString;
/////////
// * Get Local Image
/////////
[_imageCache queryDiskCacheForKey:key done:^(UIImage *image, SDImageCacheType cacheType) {
if (image) {
completedBlock(image,nil); // return block
}else{
/////////
// * Request Image
/////////
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url
options:SDWebImageProgressiveDownload
progress:^(NSInteger receivedSize, NSInteger expectedSize) {}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (finished && image){
/////////
// * Save Image
/////////
[_imageCacheProgram storeImage:image
recalculateFromImage:NO
imageData:data
forKey:key
toDisk:YES];
completedBlock(image,error); // return block
}
}];
});
}
}];

Related

Can we set local image to SDWebImage’s offline image?

I’m using
pod 'SDWebImage'
To download images in my application, I found that all the images were downloaded in “/Library/Caches/com.bundlename.bundlename/com.alamofire.imagedownloader/fsCachedData” with some unique names.
My app has offline support and I have managed CoreData Entity for the image with file path(files are in library caches directory), As I Upload my images my server will respond with my S3 file URL to download.
As I (the user who upload file) have an image file already but SDWebImage did not know that. So it will download the file again.
Any suggestion what should I do to manage it without downloading the same image again? I can not keep local path in my database as my app has sync function with multiple devices.
Thanks
I think you could use SDImageCache for this purpose.
Sample code:
NSArray<NSString*> *urls = #"aws s3 url here";
[[SDImageCache sharedImageCache] diskImageExistsWithKey:urls[0] completion:^(BOOL isInCache) {
if ( isInCache ) {
[yourImage setImage:[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:urls[0]]];
} else {
NSURL *url = [NSURL URLWithString:item.url];
[yourImage sd_setImageWithURL:url completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
[[SDImageCache sharedImageCache] storeImage:image forKey:urls[0] toDisk:YES completion:^{
}];
[yourImage setImage:image];
}];
}
}] ;

Get SDWebImage Cache Image

I would like to ask on how to get the downloaded image after the SDWebImageManager downloaded it. I have only the code to download it via URL, here's what I got:
let manager: SDWebImageManager = SDWebImageManager.sharedManager()
manager.downloadImageWithURL(NSURL(string: feedDetails.thumbnail), options: [],
progress: {(receivedSize: Int, expectedSize: Int) -> Void in
print(receivedSize)
},
completed: {(image, error, cached, finished, url) -> Void in
self.feedImage.image = image
}
)
As far as I know (I just looked up the author's Git page) there is the following method to directly access an image which is stored inside the cache-
You can use the SDImageCache to store an image explicitly to the cache with the following code:
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
Where myImage is the image you want to store and myCacheKey is a unique identifier for the image.
After you stored an image to the cache and want to use that image, just do the following:
[[SDImageCache sharedImageCache] queryDiskCacheForKey:myCacheKey done:^(UIImage *image) {
// image is not nil if image was found
}];
This code is Objective-C code, you have to "convert" it to swift yourself.
I hope I could help you!
From the SDWebImageManager class the downloadImageWithURL: method
Downloads the image at the given URL if not present in cache or return
the cached version otherwise.
So if the image is present in cache you are already retrieving it with your code, instead of downloading from the web.
Thanks for answer #beeef but SDWebImage has been updated some part of code:
Save image:
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:string] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (image && finished) {
// Cache image to disk or memory
[[SDImageCache sharedImageCache] storeImage:image forKey:#"img_key" toDisk:YES completion:^{
//save
}];
}
}];
Get image from disk cache:
[[SDImageCache sharedImageCache] queryCacheOperationForKey:#"img_key" done:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
[self.imageV setImage: image];
}];

How to set progressive image in iOS using SDWebImage?

I tried to load an image progressively in my app. Basically what I tried is while the image is loading, I want to show the fully loaded image from blurred state. I tried,
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:[NSURL URLWithString:#"https://profile.microsoft.com/RegsysProfileCenter/Images/personal_info.jpg"]
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
[self.profileBtn setImage:[UIImage imageWithData:[NSData dataWithBytes:&receivedSize length:sizeof(receivedSize)] scale:15] forState:UIControlStateNormal];
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
// [self.profileBtn setImage:image forState:UIControlStateNormal];
}
}];
Is it possible to load an image progressively using SDWebImage. Please help me with this.
You can try this implementation
+ (void)loadImageWithURLString:(NSString *)urlString forImageView:(UIImageView *)imageView {
[imageView sd_setImageWithURL:[Utilities stringToURL:urlString]
placeholderImage:[UIImage placeHolderImage]
options:SDWebImageProgressiveDownload];
}
The option says that
/**
* This flag enables progressive download, the image is displayed progressively during download as a browser would do.
* By default, the image is only displayed once completely downloaded.
*/
SDWebImageProgressiveDownload = 1 << 3,
You can show percentage of downloading on temp label or below activity indicator (can show activityindicator on image view or button till download is not completed) like in progress block,
NSInteger downloadCompleted = (receivedSize/ expectedSize) * 100;
label.text = downloadCompleted; //temp label on image view or below activityindicator
Hope this will help :)

How to use SDWebImage to cache image for multiple sized images

From my web service i am getting multiple sized images. I m loading them in TableView. When i m loading them new images which haven't cached yet flickers between placeholder and original image, or sometimes the image appear as smaller than the usual size. But just a bit scrolling down or up actually fix the problem but i want them to be at original size from the beginning. But it would appear in original shape from the next time i suppose because the picture was already cached
Initially
After a bit scrolling
My code in cellForRowAtIndexPath:
[cell.image sd_setImageWithURL:[NSURL URLWithString:[NSMutableString stringWithFormat:#"%#app/media/access/pictures?p=%#",baseurl,data.picPath]]
placeholderImage:[UIImage imageNamed: #"image_loader.gif"]];
then in heightForRowAtIndexPath:
NSURL *filePath = [NSURL URLWithString:[NSMutableString stringWithFormat:#"%#app/media/access/pictures?p=%#",baseurl,data.picPath]];
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:filePath];
UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
self.img = image;
if (self.img.size.width > CGRectGetWidth(self.view.bounds)) {
CGFloat ratio = self.img.size.height / self.img.size.width;
return CGRectGetWidth(self.view.bounds) * ratio+140;
} else {
return self.img.size.height+180;
}
I have visited https://github.com/rs/SDWebImage and in their common problem section they have mentioned about the problem but the suggested solution didn't work for me!!!
Actually what i did, i checked if the image was cached already and if not i have downloaded it asynchronously under heightForRowAtIndexPath:
if(image != NULL)
{
self.img = image;
}
else
{
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:filePath
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (image && finished) {
// do something with image
self.img = image;
}
}];
}

Cache UIImage using uitableview from url

I hava a uitableview , with custom cell containing two UImages. The logo images are taken from an online website, that's why there's a need to cache the images. Loading the image till now is made like this :
NSURL * imageURL = [NSURL URLWithString:[arra1 objectAtIndex:indexPath.row / 2]];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
NSURL * imageURL2 = [NSURL URLWithString:[arra2 objectAtIndex:indexPath.row / 2]];
NSData * imageData2 = [NSData dataWithContentsOfURL:imageURL2];
cell.ima1.image = [UIImage imageWithData:imageData];
cell.ima2.image2 = [UIImage imageWithData:imageData2];
What i learned from searching , is that dataWithContentsOfURL is not asynchronous , and while scrolling it will take a lot of time. I tried several methods but i can't seem to get to right one. This is my first time caching UIImages , i would highly appreciate a detailed explanation with implementation so i could learn aside from getting the job done.
Many Thanks
I use this Library which is just perfect
SDWebImage
You just need to #import <SDWebImage/UIImageView+WebCache.h> to your project, and you can define also the placeholder when image is being downloaded with just this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.textLabel.text = #"My Text";
return cell;
}
It also cache downloaded images and gives you great performance.
Hope it will help you!
SDWebImage, in my opinion, is the best option.
You simply include it in your app and use it like this:
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:[NSURL URLWithString:image_url]
options:0
progress:nil
completed:^(UIImage *images, NSError *error, SDImageCacheType cacheType, BOOL complete) {
myImageView.image = images;
}] ;
It download images asynchronously, so it does not block UI.
You can check these sample application
LazyTableImages - Sample application from Apple
MonoTouch-LazyTableImages
robertmryan- LazyTableImages - Explains clearly the limitations from apple's sample application.
Hope this helps.
Checkout UIImageLoader https://github.com/gngrwzrd/UIImageLoader
Easy to load an image, and you get callbacks for all the scenarios you would want to handle:
NSURL * imageURL = myURL;
[[UIImageLoader defaultLoader] loadImageWithURL:imageURL \
hasCache:^(UIImage *image, UIImageLoadSource loadedFromSource) {
//there was a cached image available. use that.
self.imageView.image = image;
} sendRequest:^(BOOL didHaveCachedImage) {
//a request is being made for the image.
if(!didHaveCachedImage) {
//there was not a cached image available, set a placeholder or do nothing.
self.loader.hidden = FALSE;
[self.loader startAnimating];
self.imageView.image = [UIImage imageNamed:#"placeholder"];
}
} requestCompleted:^(NSError *error, UIImage *image, UIImageLoadSource loadedFromSource) {
//network request finished.
[self.loader stopAnimating];
self.loader.hidden = TRUE;
if(loadedFromSource == UIImageLoadSourceNetworkToDisk) {
//the image was downloaded and saved to disk.
//since it was downloaded it has been updated since
//last cached version, or is brand new
self.imageView.image = image;
}
}];

Resources