How to draw a transparent stroke (or anyway clear part of an image) on the iPhone

后端 未结 6 1589
轻奢々
轻奢々 2020-12-02 07:14

I have a small app that allows the user to draw on the screen with the finger. I have a UIImageView where the user draws, by creating a CGContextRef

6条回答
  •  不知归路
    2020-12-02 07:34

    If you want to erase Image with touch this is my code .... the unerase part not recommended it is working but slightly slow.

    func addNewPathToImage(){
        print("Start erasing or not erasing")
    
        UIGraphicsBeginImageContextWithOptions(TopImageUIImageView.bounds.size, false, UIScreen.main.scale) // or 0 for the scale
    
    //My image is aspect fit so I need to get rect
    
        let aspect = TopImageUIImageView.image!.size.width / TopImageUIImageView.image!.size.height
        let viewRatio = TopImageUIImageView.bounds.size.width / TopImageUIImageView.bounds.size.height
    
    
        if aspect < viewRatio {
            let scale = TopImageUIImageView.bounds.size.height / TopImageUIImageView.image!.size.height
            let width = scale * TopImageUIImageView.image!.size.width
            let topLeftX = (TopImageUIImageView.bounds.size.width - width) * 0.5
            rect = CGRect(x: topLeftX, y: 0, width: width, height: TopImageUIImageView.bounds.size.height)
        }
        else {
            let scale = TopImageUIImageView.bounds.size.width / TopImageUIImageView.image!.size.width
            let height = scale * TopImageUIImageView.image!.size.height
            let topLeftY = (TopImageUIImageView.bounds.size.height - height) * 0.5
            rect = CGRect(x: 0.0, y: topLeftY, width: TopImageUIImageView.bounds.size.width, height: height)
        }
        ////
        context = UIGraphicsGetCurrentContext()
        TopImageUIImageView.image?.draw(in: rect)
        context?.setLineCap(.round)
        context?.setLineWidth(brushSize)
    
    
    
        if isErasing == true {
    
            context?.setShadow(offset: CGSize(width: 0, height: 0), blur: blurNumber)
            context?.setStrokeColor(UIColor.white.cgColor)
            context?.setBlendMode(.clear)
    
    
        }else{
    
            print("test the unerase image .... ?")
            context?.setStrokeColor(UIColor.init(patternImage: topImage.af_imageAspectScaled(toFit: CGSize(width: TopImageUIImageView.bounds.size.width, height: TopImageUIImageView.bounds.size.height))).cgColor)
    
        }
    
    
       // I am using these because I am using touch to define what to erase 
            context?.move(to: CGPoint(x: lastTouch.x, y: lastTouch.y))
            context?.addLine(to: CGPoint(x: currentTouch.x, y: currentTouch.y))
            context?.strokePath()
            context?.closePath() // when add this line or the "context?.beginPath" get ERROR CGContextClosePath: no current point.
            print("close the path")
    
    
    
    
            //get your image 
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            TopImageUIImageView.image = image
    
    }
    

提交回复
热议问题