简体   繁体   English

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

[英]Programmatically set image to UIImageView with Xcode 6.1/Swift

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)说“预期声明”图像"afternoon"是一个PNG,我的理解是PNG不需要在XCode 6.1中扩展。

Also tried just bgImage.image = UIImage(named: "afternoon") , but still get: 还尝试了bgImage.image = UIImage(named: "afternoon") ,但仍然得到:

在此输入图像描述

UPDATE 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): 好的,我已经把代码更新为UIImageViewviewDidLoad函数,但是UIImageView仍然没有显示图像(它存在于基本目录中,如下午.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)
}

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") . 由于您已将bgImage指定并链接为IBOutlet,因此无需将其初始化为UIImageView ...而是您需要做的就是设置图像属性,如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... 但是,如果它不是一个插座,并且你没有将它连接到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!)
    }
}

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 你只需要输入 - “图像”,然后你会得到一个建议框然后从列表中选择 - “Image Literal”(见附图)和

  • 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. 第二张附加图片)在你的图片资产中...或从那里选择其他图像。

在此输入图像描述

  • Now tap on square box - (You will see that square box after selecting above option) 现在点击方框 - (选择上面的选项后你会看到那个方框)

在此输入图像描述

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)

How about this; 这个怎么样;

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

where image_1 is within the assets folder as image_1. 其中image_1在assets文件夹中作为image_1。 png . png

This worked for me since i'm using a switch case to display an image slide. 这对我有用,因为我使用开关盒来显示图像幻灯片。

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!

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

In Swift 4, if the image is returned as nil. 在Swift 4中,如果图像返回为零。

Click on image, on the right hand side (Utilities) -> Check Target Membership 单击右侧的图像(实用程序) - >检查目标成员身份

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
}

You just need to drag and drop an ImageView , create the outlet action, link it, and provide an image (Xcode is going to look in your assets folder for the name you provided (here: "toronto")) 您只需要拖放ImageView ,创建插座操作,链接它并提供图像(Xcode将在您的assets文件夹中查找您提供的名称(此处:“toronto”))

In yourProject/ViewController.swift yourProject/ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imgView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        imgView.image = UIImage(named: "toronto")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM