#import "ViewController.h"
@interface ViewController ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
NSMutableData * myImageData;
int num;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
num =1;
NSInteger a =0;
CGFloat startPointX = (self.view.frame.size.width-20)/5;
CGFloat startPointY = (self.view.frame.size.width-20)/5;
for (int i=0; i<5; i++) {
for (int j=0; j<6; j++) {
UIImageView * image=[[UIImageView alloc]initWithFrame:CGRectMake(i*(startPointX+4)+2, j*startPointY, startPointX, startPointY)];
image.image=[UIImage imageNamed:@"1"];
image.tag=++a;
[self.view addSubview:image];
}
}
for (NSInteger i=a; i>=0; i--) {
// UIImageView * imageview=(UIImageView*)[self.view viewWithTag:i+1];
// imageview.image=[self notSameTime];
}
[self notSameTime];
[self blocksameTime];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark 同步下载
-(UIImage*)SameTime
{
NSString * str =@"http://a.hiphotos.baidu.com/exp/whcrop=90,80/sign=69220e04f703738dde1f5a60dc6b8d68/b21c8701a18b87d6f75cdabb060828381f30fd7c.jpg";
NSURL * url =[NSURL URLWithString:str];
NSURLRequest * request =[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];
// 其中缓存协议是个枚举类型包含:
// NSURLRequestUseProtocolCachePolicy(基础策略)
// NSURLRequestReloadIgnoringLocalCacheData(忽略本地缓存)
// NSURLRequestReturnCacheDataElseLoad(首先使用缓存,如果没有本地缓存,才从原地址下载)
// NSURLRequestReturnCacheDataDontLoad(使用本地缓存,从不下载,如果本地没有缓存,则请求失败,此策略多用于离线操作)
// NSURLRequestReloadIgnoringLocalAndRemoteCacheData(无视任何缓存策略,无论是本地的还是远程的,总是从原地址重新下载)
// NSURLRequestReloadRevalidatingCacheData(如果本地缓存是有效的则不下载,其他任何情况都从原地址重新下载)
NSURLResponse * response=nil;
NSError *error;
NSData * imageData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
UIImage * image=[UIImage imageWithData:imageData];
return image;
}
#pragma mark block异步下载
-(void)blocksameTime
{
NSString * str =@"http://a.hiphotos.baidu.com/exp/whcrop=90,80/sign=69220e04f703738dde1f5a60dc6b8d68/b21c8701a18b87d6f75cdabb060828381f30fd7c.jpg";
NSURL * url=[NSURL URLWithString:str];
NSURLRequest * request=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
NSOperationQueue * que=[[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:que completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
UIImageView* image =(UIImageView*)[self.view viewWithTag:5];
image.image=[UIImage imageWithData:data];
}];
}
#pragma mark 异步下载
-(UIImage *)notSameTime
{
NSString * str =@"http://a.hiphotos.baidu.com/exp/whcrop=90,80/sign=69220e04f703738dde1f5a60dc6b8d68/b21c8701a18b87d6f75cdabb060828381f30fd7c.jpg";
NSURL * url =[NSURL URLWithString:str];
NSURLRequest * request =[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f ];
[NSURLConnection connectionWithRequest:request delegate:self];
UIImage * image;
return image;
}
//下载结束
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImageView * imageview=(UIImageView*)[self.view viewWithTag:num];
num++;
imageview.image=[UIImage imageWithData:myImageData];
myImageData=nil;
}
//下载错误
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"发生错误=%@",error);
}
//加载到的数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[myImageData appendData:data];
}
请求过程中如果url包含中文
get请求中
NSString * urlstring=@"http://c.hiphotos.baidu.com/image/pic/item/泽锋";
urlstring=[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];post 请求中需要对请求体转码
request.HTTPBody=[@"zefeng=则分" dataUsingEncoding:NSUTF8StringEncoding];
//开始加载数据
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
myImageData=[NSMutableData data];
}