在代码中创建图像并将其显示到 UIImageView 中
Create an image in code and display it into 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!
你只是添加线条而不是为它们着色。
你可以通过调用
来为他们画颜色
ctx.strokePath()
紧接着(闭包内)
ctx.closePath()
您可以在此处阅读更多信息
https://developer.apple.com/documentation/coregraphics/cgcontext/1454490-strokepath
我正在尝试创建图像。通过检查大小,我可以看到图像已经创建。但我无法将该图像分配为 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!
你只是添加线条而不是为它们着色。
你可以通过调用
来为他们画颜色ctx.strokePath()
紧接着(闭包内)
ctx.closePath()
您可以在此处阅读更多信息
https://developer.apple.com/documentation/coregraphics/cgcontext/1454490-strokepath