//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. 【.net 深呼吸】设置序列化中的最大数据量

    欢迎收看本期的<老周吹牛>节目,由于剧组严重缺钱,故本节目无视频无声音.好,先看下面一个类声明. [DataContract] public class DemoObject { [Dat ...

  2. 【造轮子】打造一个简单的万能Excel读写工具

    大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...

  3. 背后的故事之 - 快乐的Lambda表达式(一)

    快乐的Lambda表达式(二) 自从Lambda随.NET Framework3.5出现在.NET开发者眼前以来,它已经给我们带来了太多的欣喜.它优雅,对开发者更友好,能提高开发效率,天啊!它还有可能 ...

  4. PHP源码分析-变量

    1. 变量的三要素变量名称,变量类型,变量值 那么在PHP用户态下变量类型都有哪些,如下: // Zend/zend.h #define IS_NULL 0 #define IS_LONG 1 #de ...

  5. 预览github里面的网页或dome

    1.问题所在: 之前把项目提交到github都可以在路径前面加上http://htmlpreview.github.io/?来预览demo,最近发现这种方式预览的时候加载不出来css,js(原因不详) ...

  6. 深入学习jQuery自定义插件

    原文地址:jQuery自定义插件学习 1.定义插件的方法 对象级别的插件扩展,即为jQuery类的实例增加方法, 调用:$(选择器).函数名(参数);      $(‘#id’).myPlugin(o ...

  7. 无法访问org.springframework.core.NestedRuntimeException 找不到org.springframework.core.NestedRuntimeException的类文件

    在学习springAOP时,出现如下异常: 无法访问org.springframework.core.NestedRuntimeException 找不到org.springframework.cor ...

  8. 看图理解JWT如何用于单点登录

    单点登录是我比较喜欢的一个技术解决方案,一方面他能够提高产品使用的便利性,另一方面他分离了各个应用都需要的登录服务,对性能以及工作量都有好处.自从上次研究过JWT如何应用于会话管理,加之以前的项目中也 ...

  9. SharePonit 2010 更改另存为列表模板的语言类型

    从朋友处得来一个列表模板:AccessApplicationSharePoint.stp 将其通过:网站操作----网站设置----列表模板,上传进去.然后去创建列表,发现找不到此模板. 根据多年老司 ...

  10. Maven(一)linux下安装

    1.检查是否安装JDK,并且设置了环境变量(JAVA_HOME): echo $JAVA_HOME java -version 运行结果: 显示jdk的安装路径,和java的版本,如: #jdk路径 ...