问题
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
回答1:
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
回答2:
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:
- First, you try to hit the image (with the key, maybe url) in the memory cache, but missed.
- Second, you try to find the image in the disk, missed again.
- Then, you should download the online image asynchronously, using GCD or some other methods.
- When the image is downloaded successfully, show it in the UIImageView, and cache it in the memory, write it down to the disk for future use.
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
}
}
回答3:
Use SDWebImage . its best and easy for image data caching.
来源:https://stackoverflow.com/questions/15291960/how-to-store-url-image-in-cache-memory-then-display-in-imageview-in-iphone