zoukankan      html  css  js  c++  java
  • 网络NSURLSession

    简单下载图片

        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];

  • 相关阅读:
    【JLOI 2015】城池攻占
    【BalticOI 2004】Sequence
    罗马游戏
    《STL源码剖析》STL迭代器分类
    《Effective C++》模版与泛型编程
    《Effective C++》继承与面向对象设计
    《Effective C++》实现 章节
    [C++]const_cast,dynamic_cast,reinterpret_cast,static_cast转型
    [C++]default constructor默认构造函数
    [C++]union联合体总结
  • 原文地址:https://www.cnblogs.com/damonWq/p/5361267.html
Copyright © 2011-2022 走看看