你應該做的是處理圖像的下載和高速緩存的NSObject類。實施例
接口文件
#import <Foundation/Foundation.h>
@interface ImageCache : NSObject
{
UIImage *background;
}
@property (nonatomic,retain) UIImage *background;
-(void)getImages;
-(void) storeImages;
-(void) clearCache;
@end
實施
@implementation ImageCache
@synthesize background;
- (void) cacheImage: (NSString *) ImageURLString : (NSString *)imageName
{
NSURL *ImageURL = [NSURL URLWithString: ImageURLString];
// Generate a unique path to a resource representing the image you want
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent: imageName];
// Check for file existence
if(![[NSFileManager defaultManager] fileExistsAtPath: docFile])
{
// The file doesn't exist, we should get a copy of it
// Fetch image
NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
UIImage *image = [[UIImage alloc] initWithData: data];
// Is it PNG or JPG/JPEG?
// Running the image representation function writes the data from the image to a file
if([ImageURLString rangeOfString: @".png" options: NSCaseInsensitiveSearch].location != NSNotFound)
{
[UIImagePNGRepresentation(image) writeToFile: docFile atomically: YES];
}
else if
(
[ImageURLString rangeOfString: @".jpg" options: NSCaseInsensitiveSearch].location != NSNotFound ||
[ImageURLString rangeOfString: @".jpeg" options: NSCaseInsensitiveSearch].location != NSNotFound
)
{
[UIImageJPEGRepresentation(image, 100) writeToFile: docFile atomically: YES];
}
}
}
- (UIImage *) getCachedImage : (NSString *)imageName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* cachedPath = [documentsDirectory stringByAppendingPathComponent:imageName];
UIImage *image;
// Check for a cached version
if([[NSFileManager defaultManager] fileExistsAtPath: cachedPath])
{
image = [UIImage imageWithContentsOfFile: cachedPath]; // this is the cached image
}else
{
NSLog(@"Error");
}
return image;
}
-(void)getImages
{
//I just googled background image and took first image I found for this example
NSString *backgroundURL = @"http://www.dvd-ppt-slideshow.com/images/ppt-background/background-3.jpg";
[self cacheImage:backgroundURL: @"Background"];
}
-(void) clearCache
{
//If you ever need to clear the cache for whatever reason, good to have a method to handle it
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile1 = [docDir stringByAppendingPathComponent: @"Background"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:docFile1 error:NULL];
}
-(void) storeImages
{
background = [self getCachedImage:@"Background"];
}
@end
在示例我給我只是用一個圖像。但你確實知道這個想法。現在,如果你想下載和存儲該圖像。只需在您的ViewController中導入ImageCache類,然後致電
ImageCache *cacheMe = [[ImageCache alloc] init];
[cache getImages];
[cacheMe storeImages];
UIImageView *image = [[UIImageView alloc] initWithImage:[cacheMe background]];
而這就是您所需要做的。如果你有一個致力於緩存的課程,那就更容易了。希望幫助!
編輯:更新忘了兩行代碼。我自己測試了一下,現在應該可以正常工作了
NS記錄你的路徑並檢查它是否正確。 – Apurv 2012-08-10 12:46:03
我不知道什麼「被擊中」意味着你可以提供緩存路徑和路徑的NSLog?並提供任何例外。通話結束後圖像無效嗎? – 2012-08-10 12:47:21
感謝您的回覆,我在tableview中顯示圖像,每當單元格重用圖像從本地路徑採取,以便其加載圖像 – dineshprasanna 2012-08-10 12:57:14