import UIKit
let imageURLs = [“http://www.planetware.com/photos-large/F/france-paris-eiffel-tower.jpg“, “http://adriatic-lines.com/wp-content/uploads/2015/04/canal-of-Venice.jpg“, “http://algoos.com/wp-content/uploads/2015/08/ireland-02.jpg“, “http://bdo.se/wp-content/uploads/2014/01/Stockholm1.jpg“]
class DonwnLoader {
class func downLoadImageWithURL(url: String) -> UIImage! {
let image = NSData(contentsOfURL: NSURL(string: url)!)
return UIImage(data: image!)
}
}
class ViewController: UIViewController {
@IBOutlet weak var imageView1: UIImageView!
@IBOutlet weak var imageView2: UIImageView!
@IBOutlet weak var imageView3: UIImageView!
@IBOutlet weak var imageView4: UIImageView!
@IBOutlet weak var sliderValueLabel: UILabel!
var queue = NSOperationQueue()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 didClickOnStart(sender: UIBarButtonItem) {queue = NSOperationQueue()let operation1 = NSBlockOperation { () -> Void inlet img1 = DonwnLoader.downLoadImageWithURL(imageURLs[0])NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void inself.imageView1.image = img1})}operation1.completionBlock = {print("operation1 completed! canceled: \(operation1.cancelled)")}queue.addOperation(operation1)let operation2 = NSBlockOperation { () -> Void inlet img2 = DonwnLoader.downLoadImageWithURL(imageURLs[1])NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void inself.imageView2.image = img2})}operation2.completionBlock = {print("operation2 completed! canceled: \(operation2.cancelled)")}queue.addOperation(operation2)let operation3 = NSBlockOperation { () -> Void inlet img3 = DonwnLoader.downLoadImageWithURL(imageURLs[2])NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void inself.imageView3.image = img3})}operation3.completionBlock = {print("operation3 completed! canceled: \(operation3.cancelled)")}queue.addOperation(operation3)let operation4 = NSBlockOperation { () -> Void inlet img4 = DonwnLoader.downLoadImageWithURL(imageURLs[3])NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void inself.imageView4.image = img4})}operation4.completionBlock = {print("operation4 completed! canceled: \(operation4.cancelled)")}queue.addOperation(operation4)}@IBAction func didClickOnCancel(sender: AnyObject) {queue.cancelAllOperations()}
@IBAction func sliderValueChanged(sender: UISlider) {self.sliderValueLabel.text = "\(sender.value * 100.0)"
}
}