Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

swift - 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?

question from:https://stackoverflow.com/questions/65937300/ios-painting-inside-uiview-how-change-background-color

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...