Swift 2.2 - 以编程方式初始化 UIImageView 的正确方法
Swift 2.2 - Correct way to programmatically init a UIImageView
这个非常简单的代码(我从项目中分离出来)的失败让我不知所措。
import UIKit
class ViewController: UIViewController {
weak var garageDoor: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
if let image = UIImage(named: "garagedoorclosed") {
print("image ready: \(image)")
garageDoor = UIImageView(image: image)
print("garageDoor: \(garageDoor)")
garageDoor.frame = CGRectMake(0, 0, 560, 400) // fails here!
view.addSubview(garageDoor)
}
else {
print("image not ready!")
}
}
}
我只想用名为 garagedoorclosed
的图像初始化 UIImageView。
我不知道为什么我无法用 garageDoor = UIImageView(image: image)
初始化 garageDoor
。
下面是我的截图:
如有任何意见,我们将不胜感激!
谢谢
在 viewDidLoad 中只需替换下面的代码即可。
var garageDoor: UIImageView!
garageDoor = UIImageView(frame: CGRectMake(0, 0, 560, 400))
if let image = UIImage(named: "garagedoorclosed") {
garageDoor.image = image
}
self.view.addSubview(garageDoor)
我编辑我的答案。主要问题是 garageDoor
的声明,因为 weak
意味着变量不保留对象,它将被释放。你已经删除了 weak
并且 garageDoor
会很强大。
var garageDoor: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let image = UIImage(named: "images2") {
print("image ready: \(image)")
garageDoor = UIImageView(image: image)
print("garageDoor: \(garageDoor)")
garageDoor.frame = CGRectMake(0, 0, 560, 400)
view.addSubview(garageDoor)
}
else {
print("image not ready!")
}
}
决定两个引用中哪一个应该是弱引用的经验法则:
think of the objects in the retain cycle as being in a parent-child relationship. In this relationship, the parent should maintain a strong reference (i.e., ownership of) its child, but the child should not maintain maintain a strong reference
original link.
这个非常简单的代码(我从项目中分离出来)的失败让我不知所措。
import UIKit
class ViewController: UIViewController {
weak var garageDoor: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
if let image = UIImage(named: "garagedoorclosed") {
print("image ready: \(image)")
garageDoor = UIImageView(image: image)
print("garageDoor: \(garageDoor)")
garageDoor.frame = CGRectMake(0, 0, 560, 400) // fails here!
view.addSubview(garageDoor)
}
else {
print("image not ready!")
}
}
}
我只想用名为 garagedoorclosed
的图像初始化 UIImageView。
我不知道为什么我无法用 garageDoor = UIImageView(image: image)
初始化 garageDoor
。
下面是我的截图:
如有任何意见,我们将不胜感激!
谢谢
在 viewDidLoad 中只需替换下面的代码即可。
var garageDoor: UIImageView!
garageDoor = UIImageView(frame: CGRectMake(0, 0, 560, 400))
if let image = UIImage(named: "garagedoorclosed") {
garageDoor.image = image
}
self.view.addSubview(garageDoor)
我编辑我的答案。主要问题是 garageDoor
的声明,因为 weak
意味着变量不保留对象,它将被释放。你已经删除了 weak
并且 garageDoor
会很强大。
var garageDoor: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let image = UIImage(named: "images2") {
print("image ready: \(image)")
garageDoor = UIImageView(image: image)
print("garageDoor: \(garageDoor)")
garageDoor.frame = CGRectMake(0, 0, 560, 400)
view.addSubview(garageDoor)
}
else {
print("image not ready!")
}
}
决定两个引用中哪一个应该是弱引用的经验法则:
think of the objects in the retain cycle as being in a parent-child relationship. In this relationship, the parent should maintain a strong reference (i.e., ownership of) its child, but the child should not maintain maintain a strong reference original link.