0

Right now my xib has a label and a image view. The text to be displayed is very easy to understand. However how do I add image?

let slide1:slide = Bundle.main.loadNibNamed("slide", owner: self, options: nil)?.first as! slide
    slide1.label.text = "Chicago"
 // I have a image I would like to add here. How would I do it?

1 Answers1

0

Assuming your UIImageView is named imageView and that your image is named "MyImage.jpg", you do this:

let theImage = UIImage(named: "MyImage.jpg") // create a UIImage
imageView.image = theImage // UIImageView takes a UIImage

Also note, UIImage(named:) is (1) case-sensitive and will result in a null image if not found, and (2) if your image is a PNG you do not need the file type.

One last note, if you misspell the image file and UIImage is null? The error will happen on the second line of code, not the first.