15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift4] Image from URL

Last updated at Posted at 2018-03-15

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() 
} 
15
12
0

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
  3. You can use dark theme
What you can do with signing up
15
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?