//BlockOperation

//
// ViewController.swift import UIKit class ViewController: UIViewController { @IBOutlet weak var image1: UIImageView!
@IBOutlet weak var image2: UIImageView!
@IBOutlet weak var image3: UIImageView!
@IBOutlet weak var image4: UIImageView! let imageUrls = [
"https://dn-boxueio.qbox.me/image1-big.jpg",
"https://dn-boxueio.qbox.me/image2-big.jpg",
"https://dn-boxueio.qbox.me/image3-big.jpg",
"https://dn-boxueio.qbox.me/image4-big.jpg"
] override func viewDidLoad() {
super.viewDidLoad()
// present(<#T##viewControllerToPresent: UIViewController##UIViewController#>, animated: <#T##Bool#>, completion: nil)
// Do any additional setup after loading the view, typically from a nib.
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} @IBAction func clearDownload(_ sender: UIButton) { // self.image1.image=UIImage(named: <#T##String#>) self.image1.image = nil
self.image2.image = nil
self.image3.image = nil
self.image4.image = nil URLCache.shared.removeAllCachedResponses()
} let queue = OperationQueue() @IBAction func cancelDownload(_ sender: AnyObject) {
self.queue.cancelAllOperations()
} @IBAction func downloadImages(_ sender: UIButton) { // queue.addOperation({
//
//
// let img1 = Downloader.downloadImageWithURL(self.imageUrls[0])
//
// OperationQueue.main.addOperation({
// self.image1.image = img1
// self.image1.clipsToBounds = true
// })
// })
//
//
let op1 = BlockOperation(block: {
let img1 = Downloader.downloadImageWithURL(self.imageUrls[0]) OperationQueue.main.addOperation({
self.image1.image = img1
self.image1.clipsToBounds = true
})
})
op1.completionBlock = { print("image1 downloaded") } let op2 = BlockOperation(block: {
let img2 = Downloader.downloadImageWithURL(self.imageUrls[1]) OperationQueue.main.addOperation({
self.image2.image = img2
self.image2.clipsToBounds = true
})
})
op2.completionBlock = { print("image2 downloaded") } let op3 = BlockOperation(block: {
let img3 = Downloader.downloadImageWithURL(self.imageUrls[2]) OperationQueue.main.addOperation({
self.image3.image = img3
self.image3.clipsToBounds = true
})
})
op3.completionBlock = { print("image3 downloaded") } let op4 = BlockOperation(block: {
let img4 = Downloader.downloadImageWithURL(self.imageUrls[3]) OperationQueue.main.addOperation({
self.image4.image = img4
self.image4.clipsToBounds = true
})
})
op4.completionBlock = { print("image4 downloaded") } // op3.addDependency(op4)
// op2.addDependency(op3) queue.addOperation (op1)
queue.addOperation(op4)
queue.addOperation(op3)
queue.addOperation(op2)
}
} //GCD实用代码块 import UIKit class ViewController: UIViewController { @IBOutlet weak var image1: UIImageView!
@IBOutlet weak var image2: UIImageView!
@IBOutlet weak var image3: UIImageView!
@IBOutlet weak var image4: UIImageView! let imageUrls = [
"https://dn-boxueio.qbox.me/image1-big.jpg",
"https://dn-boxueio.qbox.me/image2-big.jpg",
"https://dn-boxueio.qbox.me/image3-big.jpg",
"https://dn-boxueio.qbox.me/image4-big.jpg"
] override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} @IBAction func clearDownload(_ sender: UIButton) {
self.image1.image = nil
self.image2.image = nil
self.image3.image = nil
self.image4.image = nil URLCache.shared.removeAllCachedResponses()
} func loadImageWithURL(_ url:String) -> UIImage! {
let data = try? Data(contentsOf: URL(string: url)!)
return UIImage(data: data!) } @IBAction func downloadImages(_ sender: UIButton) { var currQueue = DispatchQueue.global(qos: .default)
// currQueue = DispatchQueue(label: "com.boxueio.images", attributes: .concurrent)
//并行
currQueue=DispatchQueue(label: "currqueue1", qos: .default, attributes: .concurrent) // dispatch_async
currQueue.async(execute: {
let img1 = self.loadImageWithURL(self.imageUrls[0])
DispatchQueue.main.async(execute: {
self.image1.image = img1
self.image1.clipsToBounds = true
})
}) currQueue.async(execute: {
let img2 = self.loadImageWithURL(self.imageUrls[1])
DispatchQueue.main.async(execute: {
self.image2.image = img2
self.image2.clipsToBounds = true
})
}) currQueue.async(execute: {
let img3 = Downloader.downloadImageWithURL(self.imageUrls[2])
DispatchQueue.main.async(execute: {
self.image3.image = img3
self.image3.clipsToBounds = true
})
}) currQueue.async(execute: {
let img4 = Downloader.downloadImageWithURL(self.imageUrls[3])
DispatchQueue.main.async(execute: {
self.image4.image = img4
self.image4.clipsToBounds = true
})
})
}
} //class图片基类 import UIKit
class Downloader { class func downloadImageWithURL(_ url:String) -> UIImage! {
let data = try? Data(contentsOf: URL(string: url)!)
return UIImage(data: data!)
}
}

【swift】BlockOperation和GCD实用代码块的更多相关文章

  1. Android实用代码块

    通过反射实现Menu显示图标 @Override public boolean onCreateOptionsMenu(Menu menu) { setIconEnable(menu, true); ...

  2. IOS开发之----代码块的使用(二)

    iOS4引入了一个新特性,支持代码块的使用,这将从根本上改变你的编程方式.代码块是对C语言的一个扩展,因此在Objective-C中完全支持.如果你学过Ruby,Python或Lisp编程语言,那么你 ...

  3. Swift中的GCD——常见的dispatch方法

    什么是GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法.该方法在Mac OS X 10.6雪豹中首次推出,并随后被引入到了iOS4.0中.GCD ...

  4. [转]iOS代码块Block

    代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返回值,特殊地,Block还可以保存一段代码,在需要 ...

  5. Android 实用代码片段

    一些不常见确又很实用的代码块. 1.精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) public static double getScreenPhysicalSize(Activity ct ...

  6. objective-c 中代码块(blocks)

    在ios4之后,引入了代码块的特性,在gcd中会经常的用到,所以决定好好的看看代码块文档,把这块总结一下.从头开始讲解代码块. 1.声明和使用代码块 一般用^操作符声明一个块变量,并作为块的开始符.而 ...

  7. IOS学习4——block代码块

    本文转载自:iOS开发-由浅至深学习block 一.关于block 在iOS 4.0之后,block横空出世,它本身封装了一段代码并将这段代码当做变量,通过block()的方式进行回调.这不免让我们想 ...

  8. Ruby学习之代码块

    代码块在其他的语言中都或多或少接触过一些,如perl中sort{$a<=>$b}keys,传入代码块实现按数值排序,在swift中用到闭包,更加深入学习到training closure. ...

  9. java子父类初始化顺序 (1)父类静态代码块(2)父类静态变量初始化(3)子类静态代码块(4)子类静态变量初始化(5)main(6)有对象开辟空间都为0(7)父类显示初始化(8)父类构造(9)子类显示初始化(10)子类构造

    标题 静态代码块与静态成员变量还要看代码的先后顺序 看程序,说出结果 结果为: x=0 看程序,说出结果 结果如下: 补充 : 静态代码块:static{ } 在JVM加载时即执行,先于主方法执行,用 ...

随机推荐

  1. [linux]阿里云主机的免登陆安全SSH配置与思考

    公司服务器使用的第三方云端服务,即阿里云,而本地需要经常去登录到服务器做相应的配置工作,鉴于此,每次登录都要使用密码是比较烦躁的,本着极速思想,我们需要配置我们的免登陆. 一 理论概述 SSH介绍 S ...

  2. Matlab 高脚杯模型切片

    前言:此文为去年我替人做的一个课题,觉得比较简洁,图形也比较美观,因此放在博文里 数据源我放到了百度云盘高脚杯数据源 有兴趣的可以下载数据,跑程序试一下.也可以单独看看代码,看下实现过程. 主函数 % ...

  3. ASP.NET MVC5+EF6+EasyUI 后台管理系统(65)-MVC WebApi 用户验证 (1)

    系列目录 前言: WebAPI主要开放数据给手机APP,其他需要得知数据的系统,或者软件应用,所以移动端与系统的数据源往往是相通的. Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能, ...

  4. 千呼万唤始出来,微软Power BI简体中文版官网终于上线了,中文文档也全了。。

    前几个月时间,研究微软Power BI技术,由于没有任何文档和资料,只能在英文官网瞎折腾,同时也发布了英文文档的相关文章:系列文章,刚好上周把文章发布完,结果简体中文版上线了.哈哈,心里有苦啊,早知道 ...

  5. 算法与数据结构(十一) 平衡二叉树(AVL树)

    今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...

  6. 玩转Vim 编辑器

    一:VIM快速入门 1.vim模式介绍 以下介绍内容来自维基百科Vim 从vi演生出来的Vim具有多种模式,这种独特的设计容易使初学者产生混淆.几乎所有的编辑器都会有插入和执行命令两种模式,并且大多数 ...

  7. Response.Redirect引起的性能问题分析

    现象: 最近做的一个系统通过单点登录(SSO) 技术验证用户登录.用户在SSO 系统上通过验证后,跳转到该系统的不同模块.而跳转的时间一直维持子啊几分钟左右. 分析步骤: 在问题复现时抓取Hang d ...

  8. spring无法读取properties文件数据

    只讲述异常点,关于怎么配置文件,这里不做说明.   1. controller中无法读取config.properties文件 controller中注入的@Value配置是从servlet-cont ...

  9. 自己实现简单Spring Ioc

    IoC则是一种 软件设计模式,简单来说Spring通过工厂+反射来实现IoC. 原理简单说明: 其实就是通过解析xml文件,通过反射创建出我们所需要的bean,再将这些bean挨个放到集合中,然后对外 ...

  10. 用angular怎么缓存父页面数据

    angular做单页面应用是一个比较好的框架,但是它有一定的入门难度,对于新手来说可能会碰到很多坑,也有许多难题,大部分仔细看文档,找社区是能解决的. 但有些问题也许资料比较少,最近遇到过一个要缓存父 ...