Since you have your bgImage assigned and linked as an IBOutlet, there is no need to initialize it as a UIImageView… instead all you need to do is set the image property like bgImage.image = UIImage(named: "afternoon")
. After running this code, the image appeared fine since it was already assigned using the outlet.
However, if it wasn’t an outlet and you didn’t have it already connected to a UIImageView object on a storyboard/xib file, then you could so something like the following…
class ViewController: UIViewController {
var bgImage: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
var image: UIImage = UIImage(named: "afternoon")!
bgImage = UIImageView(image: image)
bgImage!.frame = CGRectMake(0,0,100,200)
self.view.addSubview(bgImage!)
}
}