简单下载图片

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的更多相关文章

  1. iOS - 网络 - NSURLSession

    1.NSURLSession基础 NSURLConnection在开发中会使用的越来越少,iOS9已经将NSURLConnection废弃,现在最低版本一般适配iOS,所以也可以使用.NSURLCon ...

  2. iOS 之 网络 NSURLSession

    NSURLSession相比NSURLConnection功能更强大,是更上层的网络封装.相比而言,普通应用场景下NSURLSession没有什么优势,但是,在程序切换到后台的情况下,NSURLSes ...

  3. iOS网络NSURLSession使用详解

    一.整体介绍 NSURLSession在2013年随着iOS7的发布一起面世,苹果对它的定位是作为NSURLConnection的替代者,然后逐步将NSURLConnection退出历史舞台.现在使用 ...

  4. iOS 开发 之 编程知识点

    iOS 创建和设置pch iOS 之 时间格式与字符串转换 iOS 之 二维码生成与扫描(LBXScan) iOS 之 定时器 iOS 之 通知 iOS 之 NSString 去除前后空格和回车键 i ...

  5. iOS网络2——NSURLSession使用详解

    原文在此 一.整体介绍 NSURLSession在2013年随着iOS7的发布一起面世,苹果对它的定位是作为NSURLConnection的替代者,然后逐步将NSURLConnection退出历史舞台 ...

  6. NSURLSession网络请求

    个人感觉在网上很难找到很简单的网络请求.或许是我才疏学浅 ,  所有就有了下面这一段 , 虽然都是代码 , 但是全有注释 . //1/获取文件访问路径 NSString *path=@"ht ...

  7. 网络第三节——NSURLSession

    有的程序员老了,还没听过NSURLSession有的程序员还嫩,没用过NSURLConnection有的程序员很单纯,他只知道AFN. NSURLConnection在iOS9被宣布弃用,NSURLS ...

  8. 网络请求 __ NSURLSession

    首先配置into.plist文件 1. 添加 App Transport Security Settings , Type栏自动变为Dictionary 2. 点击左边箭头,使之向下,点击右边加号,添 ...

  9. NSURLSession访问网络数据

    1.NSMutableURLRequest的设置 //创建NSMutableURLRequest对象 NSMutableURLRequest *request = [NSMutableURLReque ...

随机推荐

  1. jQueryMobile之弹出对话框

    1:dialog-test.html <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  2. 可用版本的host

    http://blog.csdn.net/ljphhj/article/details/11939591 http://my.oschina.net/lvkun0223/blog/282356 两者的 ...

  3. nosql newsql

    http://www.cnblogs.com/end/archive/2011/10/19/2217244.html http://www.csdn.net/article/2011-09-26/30 ...

  4. Attach file to database

    D:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA databaseName.mdf databaseName.l ...

  5. Ruby on Rails Tutorial读书笔记-1

    只是怕忘了命令,全部撸一次,记个大概.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 安装Ruby之前,先要安装RVM: curl -L https://get.rvm.io | bas ...

  6. mvn 使用中的错误

    出现这种错误的时候:mvn Error building POM may not be this project's POM,报的是那个jar 包,就删除那个jar 包,重新mvn clean ins ...

  7. HDOJ 1164 Eddy's research I(拆分成素数因子)

    Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Ed ...

  8. GUID 的优缺点 uniqueidentifier

    1) 优点 同 IDENTITY 列相比,uniqueidentifier 列可以通过 NewID() 函数提前得知新增加的行 ID,为应用程序的后续处理提供了很大方便. 便于数据库移植,其它数据库中 ...

  9. SPOJ694 -- DISUBSTR 后缀树组求不相同的子串的个数

    DISUBSTR - Distinct Substrings   Given a string, we need to find the total number of its distinct su ...

  10. ASP.NET MVC4.0 部署

    EntifyFramework 5.0.0 安装 http://www.nuget.org/packages/EntityFramework/5.0.0 1. 文章,部署前的配置 http://www ...