简体   繁体   English

ios 在 UIView 内涂漆。 如何改变背景颜色?

[英]ios painting inside UIView. How change background color?

I want to make painting by finger inside UIView .我想在UIView内用手指绘画。 I am using draw function:我正在使用绘制 function:

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()
    }
}

For export I use this code:对于导出,我使用以下代码:

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
}

Line which user draws is ok.用户画的线没问题。 But I can't keep white background when export UIImage .但是导出UIImage时我不能保持白色背景。 For example, if I try to save painting result to other ImageView I got next result (white background is lost)例如,如果我尝试将绘画结果保存到其他 ImageView 我得到下一个结果(白色背景丢失)

在此处输入图像描述

I tried context.setFillColor(UIColor.white.cgColor) but it didn't work.我尝试了context.setFillColor(UIColor.white.cgColor)但它没有用。 How can I fix it?我该如何解决?

Change your custom func to:将您的自定义功能更改为:

func draw(inContext context: CGContext, rect: CGRect, bkgColor: UIColor?) {
        
    // if a background color was passed, fill the background
    if let c = bkgColor {
        context.setFillColor(c.cgColor)
        context.fill(rect)
    }
        
    // 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()
    }
}

and change your calls:并更改您的电话:

override func draw(_ rect: CGRect) {
    guard let context = UIGraphicsGetCurrentContext() else { return }
    //draw(inContext: context)
    draw(inContext: context, rect: rect, bkgColor: nil)
}

and:和:

func exportDrawing() -> UIImage? {

    // 2
    UIGraphicsBeginImageContext(frame.size)
    guard let context = UIGraphicsGetCurrentContext() else { return nil }
    
    // 3         
    //draw(inContext: context)
    draw(inContext: context, rect: CGRect(origin: .zero, size: frame.size), bkgColor: .white)
    
    // 4
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM