下面是具体步骤:
一、设置缓存策略
首先在SplitDemoAppDelegate委托代理中,实现如下代码:
在SplitDemoAppDelegate.h文件中,代码如下:
1 #import <UIKit/UIKit.h>
2
3 @class ASIDownloadCache;
4
5 @interface SplitDemoAppDelegate : NSObject <UIApplicationDelegate,UITabBarControllerDelegate> {
6
7 UIWindow *_window;
8
9 ASIDownloadCache*_downloadCache; //下载缓存策略
10
11 }
12
13 @property (nonatomic, retain) ASIDownloadCache*downloadCache;
14
15 @end
在SplitDemoAppDelegate.m文件中,代码如下:
1 #import "SplitDemoAppDelegate.h"
2
3 @implementation SplitDemoAppDelegate
4
5 @synthesize window=_window;
6
7 @synthesize downloadCache = _downloadCache;
8
9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
10
11 {
12
13 //初始化ASIDownloadCache缓存对象
14
15 ASIDownloadCache *cache = [[ASIDownloadCache alloc] init];
16
17 self.downloadCache = cache;
18
19 [cache release];
20
21
22 //路径
23
24 NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
25
26 NSString *documentDirectory = [paths objectAtIndex:0];
27
28 //设置缓存存放路径
29
30 [self.downloadCache setStoragePath:[documentDirectorystringByAppendingPathComponent:@"resource"]];
31
32 //设置缓存策略
33
34 [self.downloadCache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
35
36 // Override point for customization after application launch.
37
38 [self.window makeKeyAndVisible];
39
40 return YES;
41
42 }
43
44
45 - (void)dealloc
46
47 {
48
49 [_window release];
50
51 [_downloadCache release];
52
53 [super dealloc];
54
55 }
56
57 @end
二、创建缓存线程
这一步是创建一个NSOperation类,实现缓存的方法,代码如下:
ResourceContainer.h文件实现:
1 #import <Foundation/Foundation.h>
2
3 #import "ASIHTTPRequest.h"
4
5 #import "SplitDemoAppDelegate.h"
6
7
8
9 @interface ResourceContainer : NSOperation {
10
11 NSURL*_resourceURL; //资源请求url
12
13 NSObject*_hostObject;
14
15 SEL_resourceDidReceive; //资源接手响应方法
16
17 SplitDemoAppDelegate*_appDelegate; //应用委托对象
18
19 ASIHTTPRequest*_httpRequest;
20
21 UIImageView*_imageView;
22
23 }
24
25
26
27 @property (nonatomic, retain) NSURL*resourceURL;
28
29 @property (nonatomic, retain) NSObject*hostObject;
30
31 @property (nonatomic, assign) SELresourceDidReceive;
32
33 @property (nonatomic, assign) SplitDemoAppDelegate *appDelegate;
34
35 @property (nonatomic, retain) ASIHTTPRequest*httpRequest;
36
37 @property (nonatomic, retain) UIImageView*imageView;
38
39
40
41 //http请求回调方法
42
43 -(void)didStartHttpRequest:(ASIHTTPRequest *)request;
44
45 -(void)didFinishHttpRequest:(ASIHTTPRequest *)request;
46
47 -(void)didFailedHttpRequest:(ASIHTTPRequest *)request;
48
49
50
51 //取消资源请求
52
53 -(void)cancelReourceGet;
54
55 //资源接收回调方法
56
57 -(void)resourceDidReceive:(NSData *)resource;
58
59 @end
ResourceContainer.m文件实现:
ResourceContainer resourceURL = _resourceURL; hostObject = _hostObject; resourceDidReceive = _resourceDidReceive; appDelegate = _appDelegate; httpRequest = _httpRequest; imageView = _imageView; -()init{ (self == [super init]){ self.appDelegate = (SplitDemoAppDelegate *)[[UIApplication sharedApplication] ]; } self; } -()main{ (self.hostObject == nil) ; (self.resourceURL == nil){ [self resourceDidReceive:nil]; ; } ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:self.resourceURL] self.httpRequest = request; [self.httpRequest setDownloadCache:self.appDelegate.downloadCache]; [self.httpRequest setDelegate:self]; [self.httpRequest setDidStartSelector:@selector(didStartHttpRequest:)]; [self.httpRequest setDidFinishSelector:@selector(didFinishHttpRequest:)]; [self.httpRequest setDidFailSelector:@selector(didFailedHttpRequest:)]; [self.httpRequest startAsynchronous]; } - ()dealloc { [_resourceURL release]; [_hostObject release]; [_httpRequest release]; [_imageView release]; [super dealloc]; } -()didStartHttpRequest:(ASIHTTPRequest *)request{ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; } -()didFinishHttpRequest:(ASIHTTPRequest *)request{ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; ([request responseStatusCode] == || [request responseStatusCode] == ){ ([request didUseCachedResponse]){ NSLog(,[self.resourceURL absoluteURL]); } { NSLog(); } [self resourceDidReceive:[request responseData]]; } { [self resourceDidReceive:nil]; } } -()didFailedHttpRequest:(ASIHTTPRequest *)request{ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; [self resourceDidReceive:nil]; } -()cancelReourceGet{ [self.httpRequest cancel]; } -()resourceDidReceive:(NSData *)resource{ ([self.hostObject respondsToSelector:self.resourceDidReceive]){ (resource != nil && self.imageView != nil){ self.imageView.image = [UIImage imageWithData:resource]; } [self.hostObject performSelectorOnMainThread:self.resourceDidReceive withObject:self.imageViewwaitUntilDone:NO]; } }
到第二步,我们的缓存策略的设置,以及资源请求和接收数据方法已经构建完毕,下面介绍一下如何使用我们上面创建的NSOperation类
三、图片请求(利用上面创建的类)
这里以我的工程为例进行分析:
在DetailViewController.h声明文件中:
<UIKit/UIKit.h> DetailViewController :UIViewController { NSURL *_imageURL; NSMutableArray *_originalIndexArray; NSMutableDictionary *_originalOperationDic; NSOperationQueue *_requestImageQueue; } @property (nonatomic, retain) NSURL *imageURL; @property (nonatomic, retain) NSMutableArray *originalIndexArray; @property (nonatomic, retain) NSMutableDictionary *originalOperationDic; @property (nonatomic, retain) NSOperationQueue * requestImageQueue; -()displayProductImage; -()displayImageByIndex:(NSInteger)index ByImageURL:(NSURL *)url; -()imageDidReceive:(UIImageView *)imageView;
在DetailViewController.m实现文件中:
DetailViewController imageURL = _imageURL; originalIndexArray = _originalIndexArray; originalOperationDic = _originalOperationDic; requestImageQueue = _requestImageQueue; - ()viewDidLoad { [super viewDidLoad]; NSOperationQueue *tempQueue = [[NSOperationQueue alloc] init]; self.requsetImageQueue = tempQueue; [tempQueue release]; NSMutableArray *array = [[NSMutableArray alloc] init]; self.originalIndexArray = array; [array release]; NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; self.originalOperationDic = dic; [dic release]; } -()displayProductImage { NSURL *url = [NSURL URLWithString:]; imageCount = [self.xxxx.imageNum intValue]; ( i=; i<imageCount; i++) { NSString *str1 = ; self.imageURL = [url URLByAppendingPathComponent:str1]; [self displayImageByIndex:i ByImageURL:self.productImageURL]; } } -() displayImageByIndex:(NSInteger)index ByImageURL:(NSURL *)url { NSString *indexForString = [NSString stringWithFormat:,index]; ([self.originalIndexArray containsObject:indexForString]) { ; } UIImageView *imageView = [[UIImageView alloc] init]; imageView.tag = index; ResourceContainer *imageOperation = [[ResourceContainer alloc] init]; imageOperation.resourceURL = url; imageOperation.hostObject = self; imageOperation.resourceDidReceive = @selector(imageDidReceive:); imageOperation.imageView = imageView; [imageView release]; [self.requsetImageQueue addOperation:imageOperation]; [self.originalOperationDic setObject:imageOperation forKey:indexForString]; [imageOperation release]; } -()imageDidReceive:(UIImageView *)imageView { (imageView == nil||imageView.image == nil) { imageView.image = [UIImage imageNamed:]; } [self.openFlowView setImage:imageView.image forIndex:imageView.tag]; [self.originalIndexArray addObject:[NSString stringWithFormat:,imageView.tag]]; [self.originalOperationDic removeObjectForKey:[NSString stringWithFormat:,imageView.tag]]; } - ()dealloc { [_requestImageQueue release]; [_originalIndexArray release]; [_originalOperationDic release]; [_imageURL release]; [super dealloc]; }
经过上述步骤,我们实现了加载网络图片时缓存功能,增强了用户体验效果。代码中可能会有诸多问题,希望网友指教,有更好的缓存方法,也希望一起交流!