"Swift" blockoperation and GCD utility code blocks

Source: Internet
Author: User
Tags gcd

blockoperation////viewcontroller.swift Import uikitclass viewcontroller:uiviewcontroller {@IBOutlet weak var imag    e1: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"] Overrid e func viewdidload () {super.viewdidload ()//Present (< #T # #viewControllerToPresent: uiviewcontroller# #UIVi  Ewcontroller#&gt, 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 t    Hat can be recreated.         } @IBAction Func Cleardownload (_ Sender:uibutton) {     Self.image1.image=uiimage (named: < #T # #String #>) Self.image1.image = nil self.image2.i Mage = Nil Self.image3.image = Nil Self.image4.image = Nil URLCache.shared.removeAllCachedRes Ponses ()} Let queue = Operationqueue () @IBAction func canceldownload (_ Sender:anyobject) {Self.queu E.cancelalloperations ()} @IBAction func Downloadimages (_ Sender:uibutton) {//Queue.addo            Peration ({/////Let IMG1 = Downloader.downloadimagewithurl (Self.imageurls[0])// OperationQueue.main.addOperation ({//self.image1.image = img1//Self.imag            E1.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 = {pri NT ("Image1 downloaded")} Let Op2 = Blockoperation (block: {Let Img2 = Downloader.downloadimagew Ithurl (Self.imageurls[1]) OperationQueue.main.addOperation ({self.image2.image = img 2 Self.image2.clipsToBounds = true}) Op2.completionblock = {print ("Image2 Dow Nloaded ")} Let Op3 = Blockoperation (block: {Let IMG3 = Downloader.downloadimag Ewithurl (self.imageurls[2]) OperationQueue.main.addOperation ({self.image3.image = i MG3 self.image3.clipsToBounds = True})}) Op3.completionblock = {print ("Image3 D Ownloaded ")} Let Op4 = Blockoperation (block: {Let IMG4 = Downloader.downloadim Agewithurl (Self.imageurlS[3]) OperationQueue.main.addOperation ({self.image4.image = Img4 sel           F.image4.clipstobounds = True})}) Op4.completionblock = {print ("Image4 downloaded")} Op3.adddependency (OP4)//Op2.adddependency (OP3) queue.addoperation (OP1) Queue.addop Eration (OP4) queue.addoperation (OP3) queue.addoperation (OP2)}}//GCD Utility code block import Uikitclass 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"] Overrid e 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 t    Hat 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)//parallel CU        Rrqueue=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]) Dispatchqueu            E.main.async (execute: {self.image2.image = Img2 Self.image2.clipsToBounds = True })}) Currqueue.async (execute: {Let IMG3 = Downloader.downloadimagewithurl (self.imageur LS[2]) DispatchQueue.main.async (execute: {self.image3.image = Img3 self.image3.c Lipstobounds = True})}) Currqueue.async (execute: {Let IMG4 = Downloader.dow Nloadimagewithurl (Self.imageurls[3]) DispatchQueue.main.async (execute: {self.image4.image = img 4 self.imAge4.clipstobounds = True})})}}//class picture base class Import Uikitclass Downloader {class Func Downloa Dimagewithurl (_ url:string), uiimage! {Let data = try?        Data (Contentsof:url (string:url)!)    Return UIImage (data:data!) }}

"Swift" blockoperation and GCD utility code blocks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.