ios多线程GCD下载图片

 1 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 2 {
 3     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
 4     dispatch_async(queue, ^{
 5         NSLog(@"--download--%@", [NSThread currentThread]);
 6         // 下载图片
 7         NSURL *url = [NSURL URLWithString:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];
 8         NSData *data = [NSData dataWithContentsOfURL:url]; // 这行会比较耗时
 9         UIImage *image = [UIImage imageWithData:data];
10
11         // 回到主线程显示图片
12         dispatch_async(dispatch_get_main_queue(), ^{
13             NSLog(@"--imageView--%@", [NSThread currentThread]);
14             self.imageView.image = image;
15         });
16     });
17 }
时间: 2024-08-12 14:49:01

ios多线程GCD下载图片的相关文章

iOS 用GCD下载网络图片方法

用多线程下载网络图片会导致重复下载浪费流量的问题. 以免出现以上情况,用多线程下载图片方法可以用以下方法. 1.创建一个字典,如果下载了该图片则将用图片地址为key,图片为value保存. 2.创建一个字典,如果子线程已开始下载则用图片地址为key,图片地址为value保存. 3.如果正在下载中,显示占位图片 4.如果字典中有下载的图片,则刷新图片 //属性 @interface ViewController () @property (weak, nonatomic) IBOutlet UII

iOS多线程 GCD

iOS多线程 GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispatch_get_main_queue获取. /*! * @function dispatch_get_main_queue * * @abstract * Returns the default queue that is bound to the main thread. *

iOS多线程GCD

Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispatch_get_main_queue获取. /*! * @function dispatch_get_main_queue * * @abstract * Returns the default queue that is bound to the main thread. * * @discussi

深入IOS多线程 GCD(一)

一,什么是GCD GCD是异步执行任务的技术之一,一般将应用程序中记述的线程管理用的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适当的dispatch queue中,GCD就能生成必要的线程并计划执行任务.由于线程管理师作为系统的一部分来实现的,因此可统一管理,也可执行任务,这样就比以前的线程更有效率. 也就是说GCD用我们难以置信的非常简单的记述方法,实现了极为复杂的多线程编程,可以说这是一项划时代的技术.下面是使用了GCD源码的例子,虽然稍微抽象,但从中也能感受到GCD的威力 d

IOS 多线程GCD的使用[转载于新浪微博, 原作者:太阳石]

原文 在红黑联盟上看到一篇关于多线程GCD的教程文章,写的深入浅出,特转载于此,以备不时之需.原文链接另:补充两个GCD代码,都是Xcode snippet里面提供的:1.Dispatch After主要用于延迟执行一些代码.例子: int64_t delayInSeconds = 1.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_aft

ios多线程 -- GCD 常见用法

GCD 通信操作 #pragma mark - GCD 通信 - (void)sendMessage{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //耗时操作 [self downLoad:@"http://..."]; //回主队列刷新数据 dispatch_async(dispatch_get_main_queue(), ^{ //刷新UI操作 }); }); }

iOS 多线程 GCD part3:API

https://www.jianshu.com/p/072111f5889d 2017.03.05 22:54* 字数 1667 阅读 88评论 0喜欢 1 0. 预备知识 GCD对时间的描述有些新奇 #define NSEC_PER_SEC 1000000000ull #define NSEC_PER_MSEC 1000000ull #define USEC_PER_SEC 1000000ull #define NSEC_PER_USEC 1000ull MSEC:毫秒 USEC:微秒 NSE

IOS -多线程 - GCD - 初识

1. 什么是GCD a. 全称是Grand Center Dispatch b. 纯C语言,提供了非常多强大的函数 2. GCD的优势 a. GCD是苹果公司为多核的并行运算提出的解决方案 b. GCD会自动利用更多的CPU内核(比如双核.四核) c. GCD会自动管理线程的生命周期(创建线程.调度任务.销毁线程) d. 程序员只要告诉GCD想要执行什么任务,不需要编写任何线程管理的代码 3. GCD两个核心概念--任务和队列 任务:要执行的操作(方法) 使用block封装,block 就是一个

iOS 多线程( GCD NSThread )

1.NSThread ①.开线程的几种方式 *先创建, 后启动 //开启线程 NSThread *thread =[[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil]; //启动 [thread start]; //直接启动 [NSThread detachNewThreadSelector:@selector(<#selector#>) toTarget:self withObject:nil]; ②