码迷,mamicode.com
首页 > 编程语言 > 详细

【swift】BlockOperation和GCD实用代码块

时间:2017-01-01 22:36:58      阅读:440      评论:0      收藏:0      [点我收藏+]

标签:uiview   uibutton   ica   mat   async   create   attribute   ati   named   

//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实用代码块

标签:uiview   uibutton   ica   mat   async   create   attribute   ati   named   

原文地址:http://www.cnblogs.com/apiapia/p/6241655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!