I directly open URL image in Imageview but i want to store the images in cache memory for future reference... help me to solve my problem..
Thanks in advance
You can use SDWebImage which uses image caching...
This library provides a category for UIImageVIew
with support for remote images coming from the web.
It provides:
An UIImageView
category adding web image and cache management to the Cocoa Touch framework
An asynchronous image downloader
An asynchronous memory + disk image caching with automatic cache expiration handling
A background image decompression
A guarantee that the same URL won't be downloaded several times
A guarantee that bogus URLs won't be retried again and again
A guarantee that main thread will never be blocked
Performances! Use GCD and ARC
It's a classic problem in iOS development, I write a class OnlineImageView before, which is using NSOperationQueue -- Now GCD maybe better.
You wanna access an online image:
Some sample code :
- (void)downloadImage:(NSDictionary *)info
{
/* HOWTO: Prevent Downloading the same url many times */
/* Keep the download task one by one ? */
NSString *url = [info objectForKey:@"url"];
NSURL *imageUrl = [NSURL URLWithString:url];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
if (image) {
id theDelegate = [info objectForKey:@"delegate"];
if ([theDelegate respondsToSelector:@selector(imageDownloader:didFinishWithImage:)]) {
[theDelegate imageDownloader:self didFinishWithImage:image];
}
[cacheQueue cacheImage:image withKey:url];
} else {
#ifdef DEBUG
NSLog(@"Failed to download : %@\n", url);
#endif
}
}
Use SDWebImage . its best and easy for image data caching.