NSURLConnection下载文件

#import "ViewController.h"

@interface ViewController ()
{
    UITextField *textField;
    UIButton *button;
    UILabel *label;
    UILabel *label1;
    UIImageView *image1;
    UIImageView *image2;
    UIButton *button1;
    int flag;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    flag = 1;
    [self layout];

}
-(void)layout{
    //地址栏
    textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 40, 355, 35)];
    textField.layer.borderWidth = 2;
    textField.layer.borderColor = [UIColor blackColor].CGColor;
    textField.layer.cornerRadius = 7;
    textField.textAlignment = NSTextAlignmentCenter;
    textField.backgroundColor = [UIColor whiteColor];
    textField.text = @"http://c.hiphotos.baidu.com/zhidao/pic/item/b219ebc4b74543a930b783631e178a82b80114f4.jpg";
    button = [[UIButton alloc]initWithFrame:CGRectMake(30, 630, 315, 27)];
    [button setTitle:@"下载" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.layer.borderColor = [UIColor purpleColor].CGColor;
    button.layer.borderWidth = 2;
    button.layer.cornerRadius = 7;
    [button addTarget:self action:@selector(sendRequest) forControlEvents:UIControlEventTouchUpInside];
    label = [[UILabel alloc]initWithFrame:CGRectMake(10, 90, 70, 30)];
    label.layer.borderWidth = 2;
    label.layer.borderColor = [UIColor purpleColor].CGColor;
    label.layer.cornerRadius = 7;
    label.text = @"One";
    label.textAlignment = NSTextAlignmentCenter;
    label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 323, 70, 30)];
    label1.layer.borderWidth = 2;
    label1.layer.borderColor = [UIColor purpleColor].CGColor;
    label1.layer.cornerRadius = 7;
    label1.text = @"Two";
    label1.textAlignment = NSTextAlignmentCenter;

    image1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 122, 375, 200)];
    image1.backgroundColor = [UIColor brownColor];
    image2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 354, 375, 220)];
    image2.backgroundColor = [UIColor brownColor];
    button1 = [[UIButton alloc]initWithFrame:CGRectMake(30, 600, 315, 27)];
    [button1 setTitle:@"清空" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button1.layer.borderColor = [UIColor purpleColor].CGColor;
    button1.layer.borderWidth = 2;
    button1.layer.cornerRadius = 7;
    [button1 addTarget:self action:@selector(clean) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:label];
    [self.view addSubview:label1];
    [self.view addSubview:textField];
    [self.view addSubview:button1];
    [self.view addSubview:button];
    [self.view addSubview:image2];
    [self.view addSubview:image1];
    
}
-(void)sendRequest{
    NSString *urlStr = textField.text;
    //转换格式
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //创建url
    NSURL *url = [NSURL URLWithString:urlStr];
    //创建网络请求
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];
    //获得数据
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        //创建储存路径将文件存入
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        path = [path stringByAppendingPathComponent:@"图片007.jpg"];
        [data writeToFile:path atomically:YES];
        
        if (flag%2==0) {
            
            image2.image = [UIImage imageWithData:data];
        }
        else {
            image1.image = [UIImage imageWithData:data];
        }
        
        flag++;
    }];
    
    
}
-(void)clean{
    image2.image = nil;
    image1.image = nil;
    textField.text = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


你可能感兴趣的:(NSURLConnection下载文件)