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..
Asked
Active
Viewed 1,788 times
-2
-
add image to that view as subView, or draw image on background of your UIView : http://stackoverflow.com/a/26311986/3411787 – Mohammad Zaid Pathan Dec 19 '16 at 10:29
-
thats helps to add default image but i need to add image which getting from url – raj kumar Dec 19 '16 at 10:31
-
you could use an async library to download and set image: https://github.com/onevcat/Kingfisher or https://github.com/rs/SDWebImage – Mohammad Zaid Pathan Dec 19 '16 at 10:32
-
any other simple idea – raj kumar Dec 19 '16 at 10:39
-
@rajkumar can you share some piece of code? you tried – Museer Ahamad Ansari Dec 19 '16 at 10:51
-
i followed the method of himanshu movadiya who suggest some method – raj kumar Dec 19 '16 at 10:54
-
@rajkumar its moradiya not movadiya brother . – Himanshu Moradiya Dec 19 '16 at 11:13
3 Answers
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
-
am getting error as cannot invoke download with an argument list of string am using swift 2.2 – raj kumar Dec 19 '16 at 10:52
-
@rajkumar what kind of method you use for download image in your project ? did you update your question with that method – Himanshu Moradiya Dec 19 '16 at 10:54
-
i just followed your answer..also am having alamofire framework but dont know why thats giving that error – raj kumar Dec 19 '16 at 10:55
-
@rajkumar Did you get a image in at let image = UIImage(data: data) ? – Himanshu Moradiya Dec 19 '16 at 11:03
-
@rajkumar now i made changes check it its work fine in my project – Himanshu Moradiya Dec 19 '16 at 11:08
-
@rajkumar anytime and if problem solve then up vote answer . Happy coding. – Himanshu Moradiya Dec 19 '16 at 11:17
-
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
-
-
shows me the error of cannot invoke download with an argument list of string – raj kumar Dec 19 '16 at 10:42
-
No way we can set image to UIView. Pass url.absoluteString as an argument to that method – Sivajee Battina Dec 19 '16 at 10:45
-
yes that only am trying it gives error as shows me the error of cannot invoke download with an argument list of string....am using swift 2.2 not swift 3 – raj kumar Dec 19 '16 at 10:48
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