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

时间:2014-10-26 02:59:45

标签: ios xcode swift uiimageview uiimage

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

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

这不起作用,因为我不知道第二行应该是 yourview

问题: 如何在屏幕上显示UIImageView,而不是在故事板中进行编码

9 个答案:

答案 0 :(得分:223)

首先,您从图片文件中创建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)

答案 1 :(得分:38)

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

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

答案 2 :(得分:25)

这个答案是对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
    }

}

答案 3 :(得分:15)

您可以在一行中使用上述内容。

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

答案 4 :(得分:9)

谢谢,MEnnabah,只是在您的代码中添加您在声明声明中缺少Grass符号的地方:

amount

其他一切都是Swift 3的完美之选。

答案 5 :(得分:8)

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

答案 6 :(得分:0)

请务必放入:

imageView.translatesAutoresizingMaskIntoConstraints = false

如果您不这样做,您的图像视图将不会显示,也不要问我为什么。

答案 7 :(得分:-1)

在Swift 4.2和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
    }
}

答案 8 :(得分:-2)

快捷键4:

首先为您的UIImageView创建一个插座

@IBOutlet var infoImage: UIImageView!

然后在UIImageView中使用image属性

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