Objective-c 網絡編程2 Web請求和響應

//
//  ViewController.m
//  網絡編程2 不用代理  簡化請求響應
//
//  Created by DC017 on 15/12/9.
//  Copyright © 2015年 DC017. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    UITextField *textfield;
    UILabel* label;
    UIButton *buttonI;
    UIProgressView *progress;
    UIImageView * imageI;
    UIImageView * imageII;
    int tag;
    NSArray * array;
    long long toto;
    UIButton * buttonII;
    
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    tag = 1;
    [self buju];
    [self tupianbuju];
    
    
  
    
    
}

-(void)tupianbuju{
    imageI=[[UIImageView alloc]initWithFrame:CGRectMake(20, 190, 200, 200)];
    imageI.layer.borderWidth=2;
    imageI.layer.borderColor=[UIColor orangeColor].CGColor;
    [self.view addSubview:imageI];
    
    
    imageII=[[UIImageView alloc]initWithFrame:CGRectMake(100, 400, 200, 200)];
    imageII.layer.borderWidth=2;
    imageII.layer.borderColor=[UIColor orangeColor].CGColor;
    [self.view addSubview:imageII];
    
}

-(void)buju{
    
    
    textfield =[[UITextField alloc]initWithFrame:CGRectMake(10, 60, 300, 30)];
    textfield.layer.borderWidth=2;
    textfield.layer.cornerRadius=7;
    textfield.text=@"http://b.zol-img.com.cn/desk/bizhi/image/7/1920x1080/1449126103556.jpg";
    
    
    textfield.layer.borderColor=[UIColor blackColor].CGColor;
    [self.view addSubview:textfield];
    
    progress=[[UIProgressView alloc]initWithFrame:CGRectMake(10, 100, 300,2 )];
    progress.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:progress];
    
    label =[[UILabel alloc]initWithFrame:CGRectMake(10, 130, 100, 20)];
    label.textColor=[UIColor blackColor];
    label.text=@"請下載";
    
    [self.view addSubview:label];
    
    buttonI=[[UIButton alloc]initWithFrame:
            CGRectMake(40, 600, 50, 20)];
    
    [buttonI setTitle:@"下載" forState:UIControlStateNormal];
    [buttonI setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    buttonI.layer.borderWidth=1;
    buttonI.layer.borderColor=[UIColor redColor].CGColor;
    buttonI.layer.cornerRadius=8;
    [self.view addSubview:buttonI];
    
    [buttonI addTarget:self action:@selector(xiazai) forControlEvents:UIControlEventTouchUpInside];
    
    buttonII=[[UIButton alloc]initWithFrame:
            CGRectMake(150, 600, 50, 20)];
    
    [buttonII setTitle:@"取消" forState:UIControlStateNormal];
    [buttonII setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    buttonII.layer.borderWidth=1;
    buttonII.layer.borderColor=[UIColor redColor].CGColor;
    buttonII.layer.cornerRadius=8;
    [self.view addSubview:buttonII];
    
    [buttonII addTarget:self action:@selector(quxiao) forControlEvents:UIControlEventTouchUpInside];
    
    
}
-(void)xiazai{
    NSLog(@"下載點擊拉");
    NSString *strurl =textfield.text;
    
    strurl=[strurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL * url=[NSURL URLWithString:strurl];
    
    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:15.0f];
    
   //發送異步請求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSString *path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        NSLog(@"%@",path);
        path=[path stringByAppendingPathComponent:@"下載圖片01.jpg"];
         //保存下載內容
        [data writeToFile:path atomically:YES];
        //添加圖片
        switch (tag) {
            case 1:
                imageI.image=[UIImage imageWithData:data];
                tag = 2;
                break;
            case 2:
                imageII.image=[UIImage imageWithData:data];
                tag = 1;
                break;
                
            default:
                break;
        }
      
        
        
    }];
    
}
-(void)quxiao{
    
    textfield.text=@"";
    imageI.image=nil;
    imageII.image=nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
相關文章
相關標籤/搜索