Skip to content

Instantly share code, notes, and snippets.

@ajn123
Created December 16, 2015 03:10
Show Gist options
  • Select an option

  • Save ajn123/0ef1d8f352b1a8f985e2 to your computer and use it in GitHub Desktop.

Select an option

Save ajn123/0ef1d8f352b1a8f985e2 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// httpDownload
//
// Created by AJ Norton on 12/15/15.
// Copyright © 2015 AJ Norton. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
imageView.backgroundColor = UIColor.orangeColor()
self.view.addSubview(imageView)
let s = "https://upload.wikimedia.org/wikipedia/en/0/0b/Bumblebee-animated.png"
let url = NSURL(string: s)
let session = NSURLSession.sharedSession()
let task = session.downloadTaskWithURL(url!)
{
(url: NSURL?, res: NSURLResponse?, e: NSError?) in
let d = NSData(contentsOfURL: url!)
let image = UIImage(data: d!)
dispatch_async(dispatch_get_main_queue()) {
imageView.image = image
}
}
task.resume()
// 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.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment