一、需求:查询已有客户信息加载并从网络刷新其相关图片

二、分析:图片页面和数据分离,考虑客户可能没有图片,即将视图和数据分离,故采用IOS的观察者模式

三、代码:

  1.在原视图初始化后加入带参数的观察者

   

 1 - (id)initWithFrame:(CGRect)frame
 2 {
 3     self = [super initWithFrame:frame];
 4     if (self) {
 5         // Initialization code
 6         self.frame = frame;
 7         [self layoutContentView];
 8     }
 9     //加入异步刷新
10     [[NSNotificationCenter defaultCenter] addObserver:self
11                                              selector:@selector(updateImageWithPOAAndPOI:)
12                                                  name:CUSTOMER_CREATE_POA_PHOTO_URL
13                                                object:nil];
14     
15     [[NSNotificationCenter defaultCenter] addObserver:self
16                                              selector:@selector(updateImageWithPOAAndPOI:)
17                                                  name:CUSTOMER_CREATE_POI_PHOTO_URL
18                                                object:nil];
19     return self;
20 }
View Code

 

 

  2.同时在dealloc中删除观察者

   1 [[NSNotificationCenter defaultCenter] removeObserver:self]; 

 

  3.在需刷新图片处调用请求图片信息,设置返回方法,并在其中欧冠调用观察者

  

 1 #pragma mark  - 解析URL
 2 - (NSString*)getReplaceUrl:(NSString*)url
 3 {
 4     NSArray *seqUrl = [url componentsSeparatedByString:@"?"];
 5     NSMutableString *newUrl = [[[NSMutableString alloc] init] autorelease];
 6     bussineDataService *service = [bussineDataService sharedDataService];
 7     [newUrl appendString:service.cafUrl];
 8     
 9     if(seqUrl != nil){
10         for(int i=0;i<seqUrl.count;i++){
11             if(i>0){
12                 [newUrl appendString:@"?"];
13                 [newUrl appendString:[seqUrl objectAtIndex:i]];
14             }
15         }
16     }
17     return newUrl;
18 }
19 
20 
21 #pragma mark - 请求POI图片
22 - (void)updatePOIToImage:(NSString *)proofURL
23 {
24     bussineDataService* service = [bussineDataService sharedDataService];
25     NSURL *targetURL = [NSURL URLWithString:[self getReplaceUrl:proofURL]];
26     //如果没有先下载在查看
27     if (httpRequest != nil) {
28         [httpRequest cancel];
29         httpRequest.delegate = nil;
30         [httpRequest release];
31         httpRequest = nil;
32     }
33 
34     ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:targetURL];
35     [request addRequestHeader:@"X-API-Key" value:service.apiKey];
36     [request setTimeOutSeconds:60.0];
37     [request setDidFinishSelector:@selector(poiRequestFinished:)];
38     [request setValidatesSecureCertificate:NO];
39     [request setDelegate:self];
40     NSLog(@"poiurl-----:%@",targetURL);
41     [request startAsynchronous];
42     [request release];
43 }
44 
45 #pragma mark - 请求POA图片
46 - (void)updatePOAToImage:(NSString *)proofURL
47 {
48     bussineDataService* service = [bussineDataService sharedDataService];
49     NSURL *targetURL = [NSURL URLWithString:[self getReplaceUrl:proofURL]];
50     //如果没有先下载在查看
51     if (httpRequest != nil) {
52         [httpRequest cancel];
53         httpRequest.delegate = nil;
54         [httpRequest release];
55         httpRequest = nil;
56     }
57     
58     ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:targetURL];
59     [request addRequestHeader:@"X-API-Key" value:service.apiKey];
60     [request setTimeOutSeconds:60.0];
61     [request setDidFinishSelector:@selector(poaRequestFinished:)];
62     [request setValidatesSecureCertificate:NO];
63     [request setDelegate:self];
64     NSLog(@"poaurl-----:%@",targetURL);
65     [request startAsynchronous];
66     [request release];
67 }
68 
69 
70 
71 #pragma mark - 请求返回后异步刷新图片
72 - (void)poaRequestFinished:(ASIHTTPRequest *)request
73 {
74     NSData *fileData = [request responseData];
75     UIImage *updateImage = [UIImage imageWithData:fileData];
76     NSDictionary *data = [NSDictionary  dictionaryWithObjectsAndKeys: updateImage,@"imageData", @"poa",@"imageType",nil];
77     [[NSNotificationCenter defaultCenter] postNotificationName:CUSTOMER_CREATE_POA_PHOTO_URL
78                                                         object:nil
79                                                       userInfo:data];
80 }
81 
82 
83 #pragma mark - 请求返回后异步刷新图片
84 - (void)poiRequestFinished:(ASIHTTPRequest *)request
85 {
86     NSData *fileData = [request responseData];
87     UIImage *updateImage = [UIImage imageWithData:fileData];
88     NSDictionary *data = [NSDictionary  dictionaryWithObjectsAndKeys: updateImage,@"imageData", @"poi",@"imageType",nil];
89     [[NSNotificationCenter defaultCenter] postNotificationName:CUSTOMER_CREATE_POI_PHOTO_URL
90                                                         object:nil
91                                                       userInfo:data];
92 }
View Code

 

posted on 2014-11-11 15:58  NeverGiveUp_ZONE  阅读(442)  评论(0编辑  收藏  举报