//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. Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect)

    Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect) [TOC] 这两个方法的区别 View.ge ...

  2. jsp中出现onclick函数提示Cannot return from outside a function or method

    在使用Myeclipse10部署完项目后,原先不出错的项目,会有红色的叉叉,JSP页面会提示onclick函数错误 Cannot return from outside a function or m ...

  3. [C#] 进阶 - LINQ 标准查询操作概述

    LINQ 标准查询操作概述 序 “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法.大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了IEnumerable<T> ...

  4. ASP.NET加密和解密数据库连接字符串

    大家知道,在应用程序中进行数据库操作需要连接字符串,而如果没有连接字符串,我们就无法在应用程序中完成检索数据,创建数据等一系列的数据库操作.当有人想要获取你程序中的数据库信息,他首先看到的可能会是We ...

  5. BlockingCollection使用

    BlockingCollection是一个线程安全的生产者-消费者集合. 代码 public class BlockingTest { BlockingCollection<int> bc ...

  6. 【微信小程序开发】之如何获取免费ssl证书【图文步骤】

    微信小程序要求所有网络请求都走ssl加密,因此我们开发服务端接口需要配置为https 这篇文章介绍一下如何 在 startssl 申请一个免费的ca证书. 1. 打开网站  https://www.s ...

  7. 【HTML】Html页面跳转的5种方式

    目录结构: // contents structure [-] html实现 javascript方式实现 结合了倒数的javascript实现(IE) 解决Firefox不支持innerText的问 ...

  8. 使用DeviceOne实现微信小程序功能

    微信小程序即将推出,还没推出就火的不行了.基于微信这个巨大平台,小程序必然能有巨大成功.不过它并不能完全取代App,该开发App还得开发.如果我们自己想实现一个基于自己的APP包含类似微信的小程序功能 ...

  9. Hello bokeyuan!

    一个学习技术的年轻人 从2016/09/03进入大学学习计算机科学与技术这门学科,我已经学习了4个月了,大学的生活很枯燥,很麻烦,很多事,与我想象中的大学有很大的区别.但是这都不会影响我想要成为一个技 ...

  10. 第14章 Linux启动管理(3)_系统修复模式

    3. 系统修复模式 3.1 单用户模式 (1)在grub界面中选择第2项,并按"e键"进入编辑.并在"-quiet"后面加入" 1",即&q ...