码迷,mamicode.com
首页 > Web开发 > 详细

网络NSURLSession

时间:2016-04-06 23:05:33      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

简单下载图片

    dispatch_queue_t queue =dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{

        //1.获取网址字符串

        NSString * urlString = @"http://www.bz55.com/uploads/allimg/121230/1-121230094954.jpg";

        //2.NSString->NSURL

        NSURL * url = [NSURL URLWithString:urlString];

        //3.同步下载

        NSData * data = [NSData dataWithContentsOfURL:url];     

        UIImage * image = [UIImage imageWithData:data];

        dispatch_sync(dispatch_get_main_queue(), ^{

            self.view.backgroundColor = [UIColor colorWithPatternImage:image];

        });

    });

NSURL

    NSString * urlString = @"http://www.bz55.com/uploads/allimg/121230/1-121230094954.jpg";

    NSURL * url = [NSURL URLWithString:urlString];

    NSLog(@"Scheme: %@", [url scheme]);

    NSLog(@"Host: %@", [url host]);

    NSLog(@"Port: %@", [url port]);

    NSLog(@"Path: %@", [url path]);

    NSLog(@"Relative path: %@", [url relativePath]);

    NSLog(@"Path components as array: %@", [url pathComponents]);

技术分享

NSURLSession

    NSString * urlString = @"http://www.bz55.com/uploads/allimg/121230/1-121230094954.jpg";

    NSURL * url = [NSURL URLWithString:urlString]; 

    NSURLSessionConfiguration * defaultConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; 

    defaultConfig.timeoutIntervalForRequest = 20;

    defaultConfig.timeoutIntervalForResource = 60;

    defaultConfig.allowsCellularAccess = NO;//只能用wifi   

    NSURLSession * session = [NSURLSession sessionWithConfiguration:defaultConfig];    

    NSURLSessionDataTask * datatask =[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        NSLog(@"%@",[NSThread currentThread]);

    }]; 

    [datatask resume];

网络NSURLSession

标签:

原文地址:http://www.cnblogs.com/damonWq/p/5361267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!