首页 > 解决方案 > 在代码中创建图像并将其显示到 UIImageView

问题描述

我正在尝试创建图像。通过检查大小,我可以看到该图像已创建。但我无法将该图像分配为 UIImageView 的图像属性。请帮忙。

let imageRenderer=UIGraphicsImageRenderer(size: self.view.bounds.size)
book = imageRenderer.image { c in
    let ctx=c.cgContext
    ctx.setStrokeColor(UIColor.black.cgColor)
    ctx.setLineWidth(5)
    let x=self.view.bounds.maxX
    let y=self.view.bounds.maxY
    ctx.move(to: CGPoint(x:x/16,y:x/8))
    ctx.addLine(to: CGPoint(x: x/2, y: x/4))
    ctx.addLine(to: CGPoint(x: x-x/16, y: x/8))
    ctx.addLine(to:CGPoint(x: x-x/16, y: y-x/8))
    ctx.addLine(to: CGPoint(x: x/2, y: y-x/16))
    ctx.addLine(to: CGPoint(x: x/16, y: y-x/8))
    ctx.closePath()
}
self.image.image = book!

标签: iosswiftcocoa-touch

解决方案


您只需添加线条但不为它们绘制颜色。

你可以通过调用为他们痛苦的颜色

ctx.strokePath()

紧随其后(在封闭内)

ctx.closePath()

您可以在此处阅读更多信息

https://developer.apple.com/documentation/coregraphics/cgcontext/1454490-strokepath


推荐阅读