-2

Am developing a app using swift 2.2 in my app i need to add a background image to the view which am getting from the url i know how to add to imageview but icant add to view..

raj kumar
  • 69
  • 1
  • 7

3 Answers3

1
Alamofire.download("https://httpbin.org/image/png").responseData { response in
    if let data = response.result.value {
        let image = UIImage(data: data)
        self.view.backgroundColor = UIColor.init(patternImage: UIImage.init(CGImage: image.CGImage!))

    }
}
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
0

Directly you can't set image to UIView. First you need to set image to ImageView and add that ImageView as addSubView to UIView. If you want to load image asynchronously(for better performance), you can use Alamofire's code like below.

Alamofire.download("https://httpbin.org/image/png").responseData { response in
   if let data = response.result.value {
      let image = UIImage(data: data)
   }
}
Museer Ahamad Ansari
  • 5,414
  • 3
  • 39
  • 45
Sivajee Battina
  • 4,124
  • 2
  • 22
  • 45
0

This work for me

 let url = NSURL(string: "https://httpbin.org/image/png")
    let data = NSData(contentsOfURL: url!)
    let image = UIImage(data: data!)
    self.view.layer.contents = image?.CGImage 
Hosny
  • 821
  • 1
  • 13
  • 25