中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

NSURLRequest 簡單的網(wǎng)絡(luò)請求

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
NSURLRequest 簡單的網(wǎng)絡(luò)請求
- (void)viewDidLoad
{
    [super viewDidLoad];
     
    NSURL* url = [NSURL URLWithString:@"http://img21.mtime.cn/mg/2012/03/25/112544.76654880.jpg"];
    _imageData = [[NSMutableData alloc] init];
     
    //進度條
    _pv = [[UIProgressView alloc] initWithFrame:CGRectMake(50, 50, 220, 20)];
    [self.view addSubview:_pv];
    [_pv release];
     
     
    //創(chuàng)建一個請求
  //NSURLRequest* request = [NSURLRequest requestWithURL:url];
    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
    //發(fā)送請求
    [NSURLConnection connectionWithRequest:request delegate:self];
}
 
//接收響應(yīng)頭
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"接收到響應(yīng)頭");
    [_imageData setLength:0];
    //拿到響應(yīng)體長度
    NSHTTPURLResponse* res = (NSHTTPURLResponse*)response;
    _length = [[res.allHeaderFields objectForKey:@"Content-Length"] floatValue];
    NSLog(@"length:%f",_length);
    //是否 開啟 網(wǎng)絡(luò)開關(guān) 狀態(tài)欄的
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
 
//接收響應(yīng)體,下載數(shù)據(jù)
/*
 0 1 2 3 4 5 6 7 8 9
 0 1
     2 3 4 
           5 6 7
                 8 9
 0 1 2 3 4 5 6 7 8 9
 */
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSLog(@"接收響應(yīng)體");
    //把每次下載的數(shù)據(jù)添加到一起
    [_imageData appendData:data];
     
    //刷新進度條
    [_pv setProgress:_imageData.length / _length animated:YES];
}
 
//成功
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"成功");
    UIImage* image = [UIImage imageWithData:_imageData];
    self.view.backgroundColor = [UIColor colorWithPatternImage:image];
     
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
 
//失敗
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"失敗");
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

標簽: 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:iOS獲取設(shè)備信息

下一篇: java金額轉(zhuǎn)中文大寫