gpt4 book ai didi

ios - 如何在 UIView 中插入图片?

转载 作者:可可西里 更新时间:2023-10-31 23:56:12 30 4
gpt4 key购买 nike

下面的代码在 UIView 上打印一行。我只想知道我将编写的代码,以便能够在 View 顶部插入图像。

import UIKit

class draw: UIView {
var line = UIBezierPath()
var line1 = UIBezierPath()

func grapher() {
line1.move(to: .init(x:0, y: bounds.height / 6))
line1.addLine(to: .init(x: bounds.width, y: bounds.height / 6))
UIColor.blue.setStroke()
line1.lineWidth = 2
line1.stroke()
}

override func draw(_ rect: CGRect) {
grapher()
}
}

最佳答案

在 View 层添加UIImage

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

添加UIImage作为 subview

let image = UIImage(named: "star")
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
myView.addSubview(imageView)

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

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