ios 使用 Xcode 6.1/Swift 以编程方式将图像设置为 UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27039140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically set image to UIImageView with Xcode 6.1/Swift
提问by IlludiumPu36
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:
也试过 just bgImage.image = UIImage(named: "afternoon")
,但仍然得到:
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
仍然没有显示图像(它存在于基本目录中,作为morning.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)
}
回答by c_rath
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...相反,您只需将图像属性设置为bgImage.image = UIImage(named: "afternoon")
. 运行此代码后,图像看起来很好,因为它已经使用插座分配了。
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...
但是,如果它不是插座并且您还没有将它连接到故事板/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!)
}
}
回答by Vijay Sharma
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.
你只需要输入 - “image”,你会得到一个建议框,然后从列表中选择 -“Image Literal”(见附图)和
然后点击方块,您将能够看到
图像资产中的所有图像(参见第二张附加图片)...或从那里选择其他图像。
- Now tap on square box - (You will see that square box after selecting above option)
- 现在点击方框 - (选择上述选项后您将看到该方框)
回答by IlludiumPu36
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)
回答by dnaatwork.com
How about this;
这个怎么样;
myImageView.image=UIImage(named: "image_1")
where image_1 is within the assets folder as image_1.png.
其中 image_1 在资产文件夹中作为 image_1。PNG。
This worked for me since i'm using a switch case to display an image slide.
这对我有用,因为我使用开关盒来显示图像幻灯片。
回答by Alfie Hanssen
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!
回答by KOTIOS
With swift syntax this worked for me :
使用 swift 语法,这对我有用:
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
回答by YaBoiSandeep
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
单击右侧的图像(实用程序)-> 检查目标成员资格
回答by AnBisw
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
}
回答by rust
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")
}
}