Search code examples
iosswiftuiviewuiimageviewuiimage

ios painting inside UIView. How change background color?


I want to make painting by finger inside UIView. I am using draw 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. For example, if I try to save painting result to other ImageView I got next result (white background is lost)

enter image description here

I tried context.setFillColor(UIColor.white.cgColor) but it didn't work. How can I fix it?


Solution

  • 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
    }