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


107

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

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

这行不通,因为我不知道第二行中您的看法

问题: 如何通过编码而不是在情节提要中使UIImageView出现在屏幕上


7
与您眼前的问题无关,但是,顺便说一句,如果此图像不是您需要大量使用的图像,则可能不希望使用UIImage(named:...)。正如文档所说:“如果您有一个仅显示一次的图像文件,并希望确保不会将其添加到系统的缓存中,则应改用创建图像UIImage(contentsOfFile:...)。这样可以避免一次性图像系统映像缓存,可以改善应用程序的内存使用特征。” 仅供参考。
罗布2014年

Answers:


255

首先,您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)

39

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

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

31

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

}


12

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

1
大小和规模?
Martian2049年

1
为太多行代码而投票。会更好,把它写这样的:let imageView = UIImageView(named: "Test.jpeg"); self.view.addSubview(imageView)。2行,您使用常量而不是变量。然后使用自动约束放置它。
kakubei

10

谢谢,曼恩纳巴赫(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来说都是完美的。


6

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

此答案适用于Swift 4.2。对于像新手这样的人,扩展可能会增加对代码理解的混乱,即使它仍然很不错。
Midtownguru

-2

斯威夫特4:

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

@IBOutlet var infoImage: UIImageView!

然后在UIImageView中使用image属性

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

在swift 4.2中,第二行引发了“解开可选内容时意外发现nil”错误。
Midtownguru
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.