【发布时间】:2021-05-02 08:37:18
【问题描述】:
我想在UIView里面用手指画画。我正在使用绘图功能:
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
draw(inContext: context)
}
func draw(inContext context: CGContext) {
// 2
context.setLineWidth(5)
context.setStrokeColor(UIColor.black.cgColor)
context.setLineCap(.round)
//context.setFillColor(UIColor.white.cgColor)
// 3
for line in lineArray {
// 4
guard let firstPoint = line.first else { continue }
context.beginPath()
context.move(to: firstPoint)
// 5
for point in line.dropFirst() {
context.addLine(to: point)
}
context.strokePath()
}
}
对于导出,我使用以下代码:
func exportDrawing() -> UIImage? {
// 2
UIGraphicsBeginImageContext(frame.size)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
// 3
draw(inContext: context)
// 4
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
用户画的线没问题。但是在导出UIImage 时我不能保持白色背景。例如,如果我尝试将绘画结果保存到其他 ImageView 我得到下一个结果(白色背景丢失)
我尝试了context.setFillColor(UIColor.white.cgColor),但没有成功。
我该如何解决?
【问题讨论】:
标签: ios swift uiview uiimageview uiimage