gpt4 book ai didi

ios - 如何通过uiview插入图片

转载 作者:行者123 更新时间:2023-11-28 13:41:54 24 4
gpt4 key购买 nike

下面的代码在 UIView 上打印一行。我只想知道我将编写的代码,以便能够在 View 顶部插入图像。很多人都会point to this question但它不起作用。我试过好几次了。我在下面尝试的代码无效。

class ViewController: UIViewController {
var canVasView = UIView()

override func viewDidLoad() {
super.viewDidLoad()

let myLayer = CALayer()
let myImage = UIImage(named: "star")?.cgImage
myLayer.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
myLayer.contents = myImage
canVasView.layer.addSublayer(myLayer)
}
}

最佳答案

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

class ViewController: UIViewController {
// var canVasView = UIView()

override func viewDidLoad() {
super.viewDidLoad()

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

// Finally you'll need to give `imageView` a frame and add it your view for it to be visible:

imageView.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
// canVasView.addSubView(imageView)
self.view.bringSubviewToFront(imageView)

}

}

关于ios - 如何通过uiview插入图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55913463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com