如何以编程方式创建 UIImage 视图

我正在尝试以编程方式创建一个 UIImage 视图,我有一个新的视图,我尝试这样做

let imageName = "yourImage.png"
yourview.backgroundColor = UIColor.colorWithPatternImage(UIImage(named:imageName))

这没有工作,因为我不知道这应该是 yourview在第二行。

Question: How do I make a UIImageView appear on the screen by coding it instead of doing it in the storyboard

272883 次浏览

首先从图像文件中创建一个 UIImage,然后从中创建一个 UIImageView:

let imageName = "yourImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)

最后,你需要给 imageView一个框架,并添加你的视图,使它可见:

imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
view.addSubview(imageView)

首先创建 UIImageView,然后在 UIImageView 中添加图像。

    var imageView : UIImageView
imageView  = UIImageView(frame:CGRectMake(10, 50, 100, 300));
imageView.image = UIImage(named:"image.jpg")
self.view.addSubview(imageView)

You can use above in one line.

  let imageView = UIImageView(image: UIImage(named: "yourImage.png")!)

这个答案是更新到 Swift 3。

这就是您可以通过编程方式添加图像视图的方法,您可以在其中控制约束。

Class ViewController: UIViewController {


let someImageView: UIImageView = {
let theImageView = UIImageView()
theImageView.image = UIImage(named: "yourImage.png")
theImageView.translatesAutoresizingMaskIntoConstraints = false //You need to call this property so the image is added to your view
return theImageView
}()


override func viewDidLoad() {
super.viewDidLoad()


view.addSubview(someImageView) //This add it the view controller without constraints
someImageViewConstraints() //This function is outside the viewDidLoad function that controls the constraints
}


// do not forget the `.isActive = true` after every constraint
func someImageViewConstraints() {
someImageView.widthAnchor.constraint(equalToConstant: 180).isActive = true
someImageView.heightAnchor.constraint(equalToConstant: 180).isActive = true
someImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
someImageView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 28).isActive = true
}


}

谢谢,MEnnabah,只是为了在你的代码中增加你在声明语句中缺少 =标志的地方:

let someImageView: UIImageView = {
let theImageView = UIImageView()
theImageView.image = UIImage(named: "yourImage.png")
theImageView.translatesAutoresizingMaskIntoConstraints = false //You need to call this property so the image is added to your view
return theImageView
}()

其他一切都很完美。

在 Swift 3.0中:

var imageView : UIImageView
imageView  = UIImageView(frame:CGRect(x:10, y:50, width:100, height:300));
imageView.image = UIImage(named:"Test.jpeg")
self.view.addSubview(imageView)

迅捷4:

首先为 UIImageView 创建一个出口

@IBOutlet var infoImage: UIImageView!

然后在 UIImageView 中使用 image 属性

infoImage.image = UIImage(named: "icons8-info-white")

In Swift 4.2 and Xcode 10.1

//Create image view simply like this.
let imgView = UIImageView()
imgView.frame = CGRect(x: 200, y: 200, width: 200, height: 200)
imgView.image = UIImage(named: "yourimagename")//Assign image to ImageView
imgView.imgViewCorners()
view.addSubview(imgView)//Add image to our view


//Add image view properties like this(This is one of the way to add properties).
extension UIImageView {
//If you want only round corners
func imgViewCorners() {
layer.cornerRadius = 10
layer.borderWidth = 1.0
layer.masksToBounds = true
}
}

一定要写:

imageView.translatesAutoresizingMaskIntoConstraints = false

Your image view will not show if you don't put that, don't ask me why.