如何在UIView上插入图片? [英] How to insert image on UIView?

查看:217
本文介绍了如何在UIView上插入图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在UIView上打印一行.我只想知道我写的能够在视图顶部插入图像的代码.

The code below prints a line on a UIView. I just want to know the code that I would write to be able to insert an image on top of the 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()
    }
}

推荐答案

如果要在视图上添加图像.请使用下面的代码.

If you want to add image on view. Please use below code.

let imageName = "yourImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
self.view.addSubview(imageView)
//Imageview on Top of View
self.view.bringSubview(toFront: imageView)

如果要绘制特定形状,请使用Core Graphics

If you want to draw specific shape then go with Core Graphics

https://developer.apple.com/documentation/coregraphics

https://cocoacasts.com/drawing-shapes-in- swift-with-paintcode/

https://www.ioscreator.com/tutorials /drawing-shapes-core-graphics-tutorial-ios10

这篇关于如何在UIView上插入图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆