【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加载时即执行,先于主方法执行,用 ...
随机推荐
- 关于几个主流语音SDK的接入问题
这两周都在忙着游戏上线还有接入游戏语音,两周分别接了腾讯语音和百度语音!!! 关于腾讯语音的一些问题 由于发现腾讯语音的在录完音频后的数据是编过码的所以出现了一些问题: *不能解码(腾讯方不提供解码算 ...
- MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信
MVVM模式解析和在WPF中的实现(五) View和ViewModel的通信 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 M ...
- C# 破解 Reflector8.5
一.分析 破解.net .dll,可以使用reflector,但官方提供的reflector是需要购买的,因此,破解reflector势在必行. 二.破解Reflector具体步骤 下面为详细的破解步 ...
- ExtJS 4.2 业务开发(三)数据添加和修改
接上面的船舶管理业务,这里介绍添加和修改操作. 目录 1. 添加操作 2. 修改操作 3. 在线演示 1. 添加操作 1.1 创建AddShipWindow.js 在业务中的view目录下创建一个Ad ...
- 使用ubuntu作为web开发环境的一些感受
从ms-dos,win95,win98,winMe,winXp,vista,win7,win10我都有使用的经历,我使用时间最长的应属winxp,其次是win7,说实话,我觉得这两个系统是微软做的最好 ...
- 来吧,HTML5之基础标签(下)
<dialog> 标签 定义对话框或窗口. <dialog> 标签是 HTML 5 的新标签.目前只有 Chrome 和 Safari 6 支持 <dialog> ...
- 步入angularjs directive(指令)--准备工作熟悉hasOwnProperty
在讲解directive之前,先做一下准备工作,为何要这样呢? 因为我们不是简单的说说directive怎么用,还要知道为什么这么用!(今天我们先磨磨刀!). 首先我们讲讲js 基础的知识--hasO ...
- css元素水平居中和垂直居中的方式
关于居中的问题,一直处于疑惑不解的状态,知道的几种方法好像也不是每一次都会起到作用,所以更加迷惑.主要是不清楚该 在什么情况下采用哪种解决方法,所以,整理了一些方法,梳理一下思路,做一个总结. 1. ...
- continue break 区别
在循环中有两种循环方式 continue , break continue 只是跳出本次循环, 不在继续往下走, 还是开始下一次循环 break 将会跳出整个循环, 此循环将会被终止 count = ...
- Angular2 Hello World 之 2.0
最近angular2正式版发布了,现在就趁热接着记录一下2.0版的Hello World.据说由RC6升级到2.0代码改动不是很大,在写的时候也感觉改动不是很大,这次记录还是以asp.net core ...