Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. extension UIImageView {
  2. public func imageFromUrl(urlString: String) {
  3. if let url = NSURL(string: urlString) {
  4. let request = NSURLRequest(URL: url)
  5. let config = NSURLSessionConfiguration.defaultSessionConfiguration()
  6. let session = NSURLSession(configuration: config)
  7.  
  8. let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
  9.  
  10. if let imageData = data as NSData? {
  11. self.image = UIImage(data: imageData)
  12. }
  13.  
  14. });
  15.  
  16. // do whatever you need with the task e.g. run
  17. task.resume()
  18. }
  19. }
  20. }
  21.  
  22. extension UIImageView {
  23. func downloadImageFrom(link link:String, contentMode: UIViewContentMode) {
  24. NSURLSession.sharedSession().dataTaskWithURL( NSURL(string:link)!, completionHandler: {
  25. (data, response, error) -> Void in
  26. dispatch_async(dispatch_get_main_queue()) {
  27. self.contentMode = contentMode
  28. if let data = data { self.image = UIImage(data: data) }
  29. }
  30. }).resume()
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement