//实现的效果图
.h文件
#import <UIKit/UIKit.h>
@interface ViewController :UIViewController<NSURLConnectionDelegate>
-(NSData*) loadPictureSync:(NSString*)url; //同步
-(void)loadPicktureAsync:(NSString*)url; //异同
@end
.m文件
#import "ViewController.h"
//#import "curl/curl.h"
#define POSTURL "http://192.168.1.99/xampp/test/"
@interface ViewController ()
{
NSURLConnection *connection; //连接网络
NSMutableData *allData; //下载的数据
NSData *data1;
UIView *_view;
UIImageView *_imageView;
NSData *data2;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
_view = [[UIViewalloc]initWithFrame:CGRectMake(0,100,320,200)];
_view.backgroundColor =[UIColorclearColor];
[self.view addSubview:_view];
UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(0,300,200, 150)];
[button setTitle:@"按钮1"forState:UIControlStateNormal];
button.backgroundColor = [UIColorblueColor];
[self.view addSubview:button];
UIButton *button1 = [[UIButtonalloc]initWithFrame:CGRectMake(0,150,80, 50)];
[button1 setTitle:@"按钮2"forState:UIControlStateNormal];
button1.backgroundColor = [UIColorblueColor];
[self.view addSubview:button1];
[button addTarget:selfaction:@selector(loadPictureSync:)forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:selfaction:@selector(loadPicktureAsync:)forControlEvents:UIControlEventTouchUpInside];
// //在tmp下创建pcache目录
// NSString *tempPath = NSTemporaryDirectory();
// NSLog(@"temp目录:%@",tempPath);
// NSString *filename = [tempPath stringByAppendingPathComponent:@"pcache"];
// NSFileManager *fileMgr = [NSFileManager defaultManager];
// [fileMgr createDirectoryAtPath:filename attributes:nil];
// NSLog(@"tmp:%@",fileMgr);
}
-(NSData*) loadPictureSync:(NSString*)url //同步下载
{
NSError *error = nil;
NSString * urlString=@"http://b.hiphotos.baidu.com/pic/w%3D230/sign=756e3a96cdbf6c81f7372beb8c3cb1d7/7c1ed21b0ef41bd54482360a50da81cb38db3d7f.jpg";
NSURL *url1 = [NSURLURLWithString:urlString];
NSURLRequest *request = [[NSURLRequestalloc]initWithURL:url1];
data1 = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:&error];
_imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,320,140)];
[_imageViewsetImage:[UIImageimageWithData:data1]];
_imageView.backgroundColor = [UIColorgrayColor];
[self.view addSubview:_imageView];
[request release];
return data1;
}
-(void)loadPicktureAsync:(NSString*)url //异步下载
{
if (connection!=nil) {
[connection release];
}
if (allData!=nil) {
// [allData release];
}
NSString * urlString=@"http://g.hiphotos.baidu.com/album/w%3D1920%3Bcrop%3D0%2C0%2C1920%2C1080/sign=7dc61815d058ccbf1bbcb1332be8875d/6159252dd42a2834fa8387ac5ab5c9ea15cebf74.jpg";
NSURL *url1 = [NSURLURLWithString:urlString];
NSURLRequest *request = [[NSURLRequestalloc]initWithURL:url1cachePolicy:NSURLRequestReturnCacheDataElseLoadtimeoutInterval:50.0];//通过url,获得request;
connection = [[NSURLConnectionalloc]initWithRequest:request delegate:self];//根据request 获取connection
[request release];
}
//1、接收完HTTP协议头,开始真正接手数据时候调用
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
allData = [[NSMutableDataalloc]initWithLength:0];
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
NSInteger statuCode = [res statusCode];
if (statuCode>=400) {
NSLog(@"HTTP ERROR CODE %ld",(long)statuCode);
}
NSDictionary *dic = [res allHeaderFields];
NSLog(@"all Headr field%@",dic);
}
//2、每接收一段数据就会调用此函数
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[allDataappendData:data];//把data加到allData最后
}
//3、接收数据失败时调用,并且中断下载
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
//4、最后,如果连接成功并下载,会调用,一般在这释放一些alloc创建的内存可以对一些数据进行处理,如在这里就用data得到图片;
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *string = [[NSStringalloc]initWithData:allDataencoding:NSUTF8StringEncoding];
NSLog(@"string----->%@",string);
NSLog(@"functiono %s is calling",__func__);//获得方法名称
UIImage *image = [UIImageimageWithData:allData];
UIImageView *_imageView1 = [[UIImageViewalloc]initWithFrame:CGRectMake(0,200,80, 80)];
[_imageView1 setImage:image];
[self.view addSubview:_imageView1];
[_imageView1 release];
[string release];
[allData release];
}
-(void)writeToFile:(NSData *)data //接收得数据写入文件
{
// //在tmp下创建pcache目录
// NSString *tempPath = NSTemporaryDirectory();
// NSLog(@"temp目录:%@",tempPath);
// NSString *filename = [tempPath stringByAppendingPathComponent:@"pcache"];
// NSFileManager *fileMgr = [NSFileManager defaultManager];
// [fileMgr createDirectoryAtPath:filename attributes:nil];
// NSLog(@"tmp:%@",fileMgr);
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
附图一张,button1 触发同步下载 button2触发异步下载