【swift】BlockOperation和GCD实用代码块
//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实用代码块的更多相关文章
- Android实用代码块
通过反射实现Menu显示图标 @Override public boolean onCreateOptionsMenu(Menu menu) { setIconEnable(menu, true); ...
- IOS开发之----代码块的使用(二)
iOS4引入了一个新特性,支持代码块的使用,这将从根本上改变你的编程方式.代码块是对C语言的一个扩展,因此在Objective-C中完全支持.如果你学过Ruby,Python或Lisp编程语言,那么你 ...
- Swift中的GCD——常见的dispatch方法
什么是GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法.该方法在Mac OS X 10.6雪豹中首次推出,并随后被引入到了iOS4.0中.GCD ...
- [转]iOS代码块Block
代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返回值,特殊地,Block还可以保存一段代码,在需要 ...
- Android 实用代码片段
一些不常见确又很实用的代码块. 1.精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) public static double getScreenPhysicalSize(Activity ct ...
- objective-c 中代码块(blocks)
在ios4之后,引入了代码块的特性,在gcd中会经常的用到,所以决定好好的看看代码块文档,把这块总结一下.从头开始讲解代码块. 1.声明和使用代码块 一般用^操作符声明一个块变量,并作为块的开始符.而 ...
- IOS学习4——block代码块
本文转载自:iOS开发-由浅至深学习block 一.关于block 在iOS 4.0之后,block横空出世,它本身封装了一段代码并将这段代码当做变量,通过block()的方式进行回调.这不免让我们想 ...
- Ruby学习之代码块
代码块在其他的语言中都或多或少接触过一些,如perl中sort{$a<=>$b}keys,传入代码块实现按数值排序,在swift中用到闭包,更加深入学习到training closure. ...
- java子父类初始化顺序 (1)父类静态代码块(2)父类静态变量初始化(3)子类静态代码块(4)子类静态变量初始化(5)main(6)有对象开辟空间都为0(7)父类显示初始化(8)父类构造(9)子类显示初始化(10)子类构造
标题 静态代码块与静态成员变量还要看代码的先后顺序 看程序,说出结果 结果为: x=0 看程序,说出结果 结果如下: 补充 : 静态代码块:static{ } 在JVM加载时即执行,先于主方法执行,用 ...
随机推荐
- Sublime Text3安装JsHint
介绍 Sublime Text3使用jshint依赖Nodejs,SublimeLinter和Sublimelinter-jshint. NodeJs的安装省略. 安装SublimeLinter Su ...
- C# MVC 5 - 生命周期(应用程序生命周期&请求生命周期)
本文是根据网上的文章总结的. 1.介绍 本文讨论ASP.Net MVC框架MVC的请求生命周期. MVC有两个生命周期,一为应用程序生命周期,二为请求生命周期. 2.应用程序生命周期 应用程序生命周期 ...
- .Net多线程编程—预备知识
1 基本概念 共享内存的多核架构:一个单独的封装包内封装了多个互相连接的未处理器,且所有内核都可以访问主内存.共享内存的多核系统的一些微架构,例如内核暂停功能,超频. 内核暂停功能:当使用内核不多的时 ...
- python爬取github数据
爬虫流程 在上周写完用scrapy爬去知乎用户信息的爬虫之后,github上star个数一下就在公司小组内部排的上名次了,我还信誓旦旦的跟上级吹牛皮说如果再写一个,都不好意思和你再提star了,怕你们 ...
- Node.js:path、url、querystring模块
Path模块 该模块提供了对文件或目录路径处理的方法,使用require('path')引用. 1.获取文件路径最后部分basename 使用basename(path[,ext])方法来获取路径的最 ...
- .NET跨平台之运行与Linux上的Jexus服务器
谈及.NET跨平台,已经不是什么稀奇的事儿.今天我们就以Jexus服务器的部署为例.简单示范下.在这里,我用VMWare虚拟机来搭建Linux运行环境. Linux,我们选择CentOS7.大家可以前 ...
- UVA-146 ID Codes
It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exerc ...
- isEmpty和isNull()区别
isEmpty和isNull()区别一个NULL字符串一定是一个空串,一个空串未必是一个NULL字符串例如:QString().isNull(): //结果为trueQString().isEm ...
- ResponsibleChain(责任链模式)
/** * 责任链模式 * @author TMAC-J * 老板讲任务交给CTO,CTO自然不会亲自去做,又把人物分配给项目经理,项目经理再把任务分配给组长,组长再分配给个人 * 如果中途哪个环节出 ...
- IIS启动失败,启动Windows Process Activation Service时,出现错误13:数据无效 ;HTTP 错误 401.2 - Unauthorized 由于身份验证头无效,您无权查看此页
因为修改过管理员账号的密码后重启服务器导致IIS无法启动,出现已下异常 1.解决:"启动Windows Process Activation Service时,出现错误13:数据无效&quo ...