Programmatically set image for UIImageView with Xcode 6.1 / Swift

I am trying to install UIImageView programmatically in Xcode 6.1:

@IBOutlet weak var bgImage: UIImageView! var image : UIImage = UIImage(named:"afternoon")! bgImage = UIImageView(image: image) bgImage.frame = CGRect(x: 0, y: 0, width: 100, height: 200) view.addSubview(bgImage) 

But Xcode says "expected announcement" with bgImage = UIImageView(image: image) The image "afternoon" is a PNG, and I understand that PNG does not need to be expanded in Xcode 6.1.

Also tried just bgImage.image = UIImage(named: "afternoon") , but still get:

enter image description here

UPDATE

Ok, I put the code for updating the UIImageView in the viewDidLoad function, but the UIImageView still doesn’t show the image (which exists in the base directory as day UIImageView ):

 @IBOutlet weak var bgImage: UIImageView! @IBOutlet weak var dateLabel: UILabel! @IBOutlet weak var timeLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. updateTime() var timer = NSTimer() let aSelector : Selector = "updateTime" timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: aSelector, userInfo: nil, repeats: true) var image : UIImage = UIImage(named:"afternoon")! bgImage = UIImageView(image: image) } 
+75
ios xcode swift
Nov 20 '14 at 12:02
source share
9 answers

Since you have your bgImage assigned and connected as an IBOutlet, there is no need to initialize it as a UIImageView ... instead, all you have to do is set the image property, for example bgImage.image = UIImage(named: "afternoon") . After running this code, the image turned out to be beautiful, because it was already assigned using the outlet.

enter image description here

However, if it was not an output and you did not already have it connected to the UIImageView object in the / xib storyboard file, then you could 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!) } } 
+138
Nov 21 '14 at 15:03
source share

In xcode 8, you can directly select an image from the selection window (NEW) ...

  • You just need to type - “image”, and you will receive an offer then select “Image Literature” from the list (see attached image) and

  • then click on the square, you can see all the images (see the second attached image) that are in your images ... or select another image from there.

enter image description here

  • Now touch the square square - (you will see this square box after selecting above)

enter image description here

+41
Sep 28 '16 at 11:54 on
source share

OK, it works with this (creating a UIImageView programmatically):

 var imageViewObject :UIImageView imageViewObject = UIImageView(frame:CGRectMake(0, 0, 600, 600)) imageViewObject.image = UIImage(named:"afternoon") self.view.addSubview(imageViewObject) self.view.sendSubviewToBack(imageViewObject) 
+8
Nov 21 '14 at 2:55
source share

How about this one;

 myImageView.image=UIImage(named: "image_1") 

where image_1 is in the resource folder as image_1. png .

This worked for me since I am using a switch case to display a slide image.

+6
Jun 10 '16 at 3:20
source share

This code is in the wrong place:

 var image : UIImage = UIImage(named:"afternoon")! bgImage = UIImageView(image: image) bgImage.frame = CGRect(x: 0, y: 0, width: 100, height: 200) view.addSubview(bgImage) 

You must put it inside the function. I recommend moving it inside the viewDidLoad function.

In general, the only code you can add inside a class that is not part of a function is variable declarations:

 @IBOutlet weak var bgImage: UIImageView! 
+4
Nov 20 '14 at 13:19
source share

With quick syntax, this worked for me:

  let leftImageView = UIImageView() leftImageView.image = UIImage(named: "email") let leftView = UIView() leftView.addSubview(leftImageView) leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 40) leftImageView.frame = CGRect(x: 10, y: 10, width: 20, height: 20) userNameTextField.leftViewMode = .always userNameTextField.leftView = leftView 
+1
Nov 28 '16 at 5:36
source share

In Swift 4, if the image returns as zero.

Click on the image on the right side (Utilities) → Check Target Membership

+1
Jul 25. '17 at 11:19 on
source share

If you want to do it the way you showed in your question, this is the way to make it inline

 class YourClass: UIViewController{ @IBOutlet weak var tableView: UITableView! //other IBOutlets //THIS is how you declare a UIImageView inline let placeholderImage : UIImageView = { let placeholderImage = UIImageView(image: UIImage(named: "nophoto")) placeholderImage.contentMode = .scaleAspectFill return placeholderImage }() var someVariable: String! var someOtherVariable: Int! func someMethod(){ //method code } //and so on } 
+1
Jan 17 '18 at 21:35
source share

You just need to drag the ImageView , create a socket action, bind it and provide the image (Xcode will look in the folder of your assets name you specified (here: "toronto"))

In yourProject/ViewController.swift

 import UIKit class ViewController: UIViewController { @IBOutlet weak var imgView: UIImageView! override func viewDidLoad() { super.viewDidLoad() imgView.image = UIImage(named: "toronto") } } 
0
Jul 18 '19 at 7:54
source share



All Articles