search
LoginSignup
12
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

[Swift4] Image from URL

1. do, catch

      let url = URL(string: user.imageURL)
      do {
            let data = try Data(contentsOf: url!)
            let image = UIImage(data: data)

       }catch let err {
            print("Error : \(err.localizedDescription)")
       }

2. URLSession

 if let image = imageURL { 
   let url = URL(string: image) 
   URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in 

      if error != nil { 
         print(error!) 
         return 
      } 

      DispatchQueue.main.async { 
         imageView.image = UIImage(data: data!) 
      } 
   }).resume() 
} 

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
12
Help us understand the problem. What are the problem?