Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- let downloadA = createOperation("thread1", "url1")
- let downloadB = createOperation("thread2", "url2")
- let queue = OperationQueue.init()
- queue.maxConcurrentOperationCount = 1
- queue.addOperation(downloadA)
- queue.addOperation(downloadB)
- }
- func createOperation(_ name: String, _ url: String) -> BlockOperation {
- let download = BlockOperation.init {
- //下载图片
- print("\(name) start download \(url)")
- // 下载完成后,回主线程显示图片
- self.perform(#selector(self.showImage(_:)), on: Thread.main, with: nil, waitUntilDone: true)
- }
- download.name = name
- return download
- }
- @objc func showImage(_ image: UIImage?) {
- }
- }
Add Comment
Please, Sign In to add comment