NSURLConnection下载文件

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
#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


标签: isp 网络

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:UITextField输入有emoji表情时的字数限定

下一篇:JavaScript多继承的实现