使用XCode 6.1/Swift以编程方式将图像设置为UIImageView

时间:2023-01-22 18:10:13

I'm trying to set UIImageView programmatically in XCode 6.1:

我试图在XCode 6.1中以编程方式设置UIImageView:

@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 is saying "expected declaration" with bgImage = UIImageView(image: image) Image "afternoon"is a PNG, and my understanding is PNG does not need an extension in XCode 6.1.

但是Xcode说的是bgImage = UIImageView(image: image) image "afternoon"是PNG,我的理解是PNG在Xcode 6.1中不需要扩展。

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

也只是bgImage。image = UIImage(命名:"afternoon"),但仍然得到:

使用XCode 6.1/Swift以编程方式将图像设置为UIImageView

UPDATE

更新

OK, I have put the code to update UIImageView into the viewDidLoad function, but UIImageView is still not showing the image (which exists in the base directory as afternoon.png):

好的,我已经将更新UIImageView的代码放到viewDidLoad函数中,但是UIImageView仍然没有显示图像(它在基目录中为afternoon.png):

 @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)

    }

8 个解决方案

#1


117  

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.

既然已经将bgImage分配并链接为IBOutlet,就不需要将它初始化为UIImageView…相反,您需要做的只是设置image属性,如bgImage。形象=用户界面图像(命名为:“下午”)。运行此代码后,图像看起来很好,因为它已经使用outlet分配了。

使用XCode 6.1/Swift以编程方式将图像设置为UIImageView

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...

但是,如果它不是一个outlet,而且你还没有将它连接到storyboard/xib文件上的UIImageView对象,那么你可以如下所示。

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!)
    }
}

#2


24  

In xcode 8 you can directly choose image from the selection window (NEW)...

在xcode 8中,您可以直接从选择窗口(新建)中选择图像。

  • You just need to type - "image" and you will get a suggestion box then select -"Image Literal" from list (see in attached picture) and

    你只需要键入“图像”,你就会得到一个建议框,然后从列表中选择“图像文字”(见附图)。

  • then tap on the square you will be able to see all images(see in
    second attached picture) which are in your image assets... or select other image from there.

    然后点击方块,你就可以看到所有的图片(见附图),它们都在你的图片资产中……或者从那里选择其他图像。

使用XCode 6.1/Swift以编程方式将图像设置为UIImageView

  • Now tap on square box - (You will see that square box after selecting above option)
  • 现在点击方框-(在选择以上选项后,你将看到方块方块)

使用XCode 6.1/Swift以编程方式将图像设置为UIImageView

#3


6  

OK, got it working with this (creating the UIImageView programmatically):

好的,让它和这个一起工作(以编程方式创建UIImageView):

var imageViewObject :UIImageView

imageViewObject = UIImageView(frame:CGRectMake(0, 0, 600, 600))

imageViewObject.image = UIImage(named:"afternoon")

self.view.addSubview(imageViewObject)

self.view.sendSubviewToBack(imageViewObject)

#4


4  

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 place it inside a function. I recommend moving it inside the viewDidLoad function.

你必须把它放在一个函数里面。我建议在viewDidLoad函数中移动它。

In general, the only code you can add within the class that's not inside of a function are variable declarations like:

一般来说,在函数之外的类中可以添加的唯一代码是变量声明,比如:

@IBOutlet weak var bgImage: UIImageView!

#5


4  

How about this;

这个怎么样;

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

where image_1 is within the assets folder as image_1.png.

其中image_1在assets文件夹中的位置为image_1.png。

This worked for me since i'm using a switch case to display an image slide.

这对我很有用,因为我使用一个开关盒来显示图像幻灯片。

#6


1  

With swift 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

#7


1  

In Swift 4, if the image is returned as nil.

在Swift 4中,如果图像返回为nil。

Click on image, on the right hand side (Utilities) -> Check Target Membership

点击图像,在右边(实用工具)->检查目标成员

#8


1  

If you want to do it the way you showed in your question, this is a way to do 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


117  

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.

既然已经将bgImage分配并链接为IBOutlet,就不需要将它初始化为UIImageView…相反,您需要做的只是设置image属性,如bgImage。形象=用户界面图像(命名为:“下午”)。运行此代码后,图像看起来很好,因为它已经使用outlet分配了。

使用XCode 6.1/Swift以编程方式将图像设置为UIImageView

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...

但是,如果它不是一个outlet,而且你还没有将它连接到storyboard/xib文件上的UIImageView对象,那么你可以如下所示。

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!)
    }
}

#2


24  

In xcode 8 you can directly choose image from the selection window (NEW)...

在xcode 8中,您可以直接从选择窗口(新建)中选择图像。

  • You just need to type - "image" and you will get a suggestion box then select -"Image Literal" from list (see in attached picture) and

    你只需要键入“图像”,你就会得到一个建议框,然后从列表中选择“图像文字”(见附图)。

  • then tap on the square you will be able to see all images(see in
    second attached picture) which are in your image assets... or select other image from there.

    然后点击方块,你就可以看到所有的图片(见附图),它们都在你的图片资产中……或者从那里选择其他图像。

使用XCode 6.1/Swift以编程方式将图像设置为UIImageView

  • Now tap on square box - (You will see that square box after selecting above option)
  • 现在点击方框-(在选择以上选项后,你将看到方块方块)

使用XCode 6.1/Swift以编程方式将图像设置为UIImageView

#3


6  

OK, got it working with this (creating the UIImageView programmatically):

好的,让它和这个一起工作(以编程方式创建UIImageView):

var imageViewObject :UIImageView

imageViewObject = UIImageView(frame:CGRectMake(0, 0, 600, 600))

imageViewObject.image = UIImage(named:"afternoon")

self.view.addSubview(imageViewObject)

self.view.sendSubviewToBack(imageViewObject)

#4


4  

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 place it inside a function. I recommend moving it inside the viewDidLoad function.

你必须把它放在一个函数里面。我建议在viewDidLoad函数中移动它。

In general, the only code you can add within the class that's not inside of a function are variable declarations like:

一般来说,在函数之外的类中可以添加的唯一代码是变量声明,比如:

@IBOutlet weak var bgImage: UIImageView!

#5


4  

How about this;

这个怎么样;

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

where image_1 is within the assets folder as image_1.png.

其中image_1在assets文件夹中的位置为image_1.png。

This worked for me since i'm using a switch case to display an image slide.

这对我很有用,因为我使用一个开关盒来显示图像幻灯片。

#6


1  

With swift 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

#7


1  

In Swift 4, if the image is returned as nil.

在Swift 4中,如果图像返回为nil。

Click on image, on the right hand side (Utilities) -> Check Target Membership

点击图像,在右边(实用工具)->检查目标成员

#8


1  

If you want to do it the way you showed in your question, this is a way to do 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
}