NSURLConnection下載文件

flkt9867 8年前發布 | 4K 次閱讀 C/C++ Objective-C

#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 = @"

    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</pre>


 本文由用戶 flkt9867 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!