Guest User

Untitled

a guest
Mar 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. extension UIImageView {
  2. public func imageFromURL(urlString: String) {
  3. let activityIndicator = self.showActivityIndicator()
  4.  
  5. URLSession.shared.dataTask(with: NSURL(string: urlString)! as URL, completionHandler: { (data, response, error) -> Void in
  6.  
  7. if error != nil {
  8. print(error ?? "No Error")
  9. return
  10. }
  11. DispatchQueue.main.async(execute: { () -> Void in
  12. if let mimeType = response?.mimeType, mimeType == "text/html" {
  13. let imageViewHeight = self.frame.size.height
  14. let noDataImageViewHeight = floor(imageViewHeight / 5)
  15. let noDataImageViewWidth = floor(noDataImageViewHeight * 0.9)
  16. self.image = UIImage(named:"nodata-file")?.resizeImage(targetSize: CGSize(width: noDataImageViewWidth, height: noDataImageViewHeight))
  17. self.contentMode = .center
  18. } else {
  19. let image = UIImage(data: data!)
  20. self.image = image
  21. }
  22. activityIndicator.stopAnimating()
  23. })
  24.  
  25. }).resume()
  26. }
  27. }
Add Comment
Please, Sign In to add comment