Getting Image from URL Objective C

前端 未结 5 1785
甜味超标
甜味超标 2020-12-04 08:13

I\'m trying to get an image from an URL and it doesn\'t seem to be working for me. Can someone point me in the right direction?

Here is my code:

NSUR         


        
5条回答
  •  清歌不尽
    2020-12-04 08:37

    In Swift 3 and 4

    let theURL = URL(string:"https://exampleURL.com")
    let imagedData = NSData(contentsOf: theURL!)!
    let theImage = UIImage(data: imagedData as Data)
    cell.theImageView.image = theImage
    

    This will be done in the main thread.

    And to perform the same in asynchronous/background thread

       DispatchQueue.main.async(){
          let theURL = URL(string:"https://exampleURL.com")
          let imagedData = NSData(contentsOf: theURL!)!
          let theImage = UIImage(data: imagedData as Data)
       }
       cell.theImageView.image = theImage
    

提交回复
热议问题