@IBAction func simpleAsynchronousDownload(_ sender: UIBarButtonItem){ let url = URL(string: BigImages.shark.rawValue) let downloadQueue = DispatchQueue(label: "download", attributes: []) downloadQueue.async {() -> Void in let imgData = try? Data(contentsOf: url!) let image = UIImage(data: imgData!) DispatchQueue.main.async(execute: {()-> Void in self.photoView.image = image }) } }
func withBigImage(completionHandler handler: @escaping(_ image: UIImage) -> Void){ DispatchQueue.global(qos: .userInitiated).async{() -> Void in if let url = URL(string: BigImages.whale.rawValue), let imgData = try? Data(contentsOf: url), let img = UIImage(data: imgData){ // all set and done, run the completion closure! DispatchQueue.main.async(execute: {() -> Void in handler(img) }) } } }