-3

I need to display the background image of a button by giving the image source from an external URL instead of downloading the image. Is there a possible way to do this in swift.

Tharindu Pradeep
  • 1,152
  • 11
  • 16

2 Answers2

2

You can use SDWebImage POD.

yourBtn.sd_setImage(with: <#T##URL?#>, for: <#T##UIControl.State#>, completed: <#T##SDExternalCompletionBlock?##SDExternalCompletionBlock?##(UIImage?, Error?, SDImageCacheType, URL?) -> Void#>)
Taimoor Suleman
  • 1,588
  • 14
  • 29
-1

First you need to get the image data from external URL and then use the image

if let url = URL(string: "YOUR-IMAGE-URL-HERE") {
            do{
                let data = try Data(contentsOf: url)
                self.imageVar = UIImage(data: data)
                if let myImage: UIImage = self.imageVar {
                    YOUR-BUTTON-NAME.setImage(myImage, for: .normal)
                }
            }catch {
                print("error")
            }
        }
Saurabh
  • 745
  • 1
  • 9
  • 33