美文网首页
绘制透明圆形遮罩层和截取圆形中的图片

绘制透明圆形遮罩层和截取圆形中的图片

作者: leafu | 来源:发表于2022-08-18 13:55 被阅读0次
    /// 截取遮罩层中镂空部分的内容生成图片
           @objc func handleSceenShot()  {
               UIGraphicsBeginImageContext(self.view.size)
               let context = UIGraphicsGetCurrentContext()
               let circlepath = UIBezierPath(roundedRect: CGRect(x: screenWidth() * 0.5 - 150, y: screenHight() * 0.5 - 150, width: 300, height:300), cornerRadius: 150)
               circlepath.addClip()
               self.view.layer .render(in: context!)
               let img = UIGraphicsGetImageFromCurrentImageContext()
               UIGraphicsEndImageContext()
               guard self.getImgFinish != nil else {
                   return
               }
               guard img != nil else {return}
               self.getImgFinish!(img)
            }
    
    /// 绘制圆形遮罩层
        func drawRoundLayer() {
            let bezierPath = UIBezierPath(roundedRect: self.view.bounds, cornerRadius: 0)
            let circlePath = UIBezierPath(roundedRect: CGRect(x: screenWidth() * 0.5 - 150, y: screenHight() * 0.5 - 150, width: 300, height:300), cornerRadius: 150)
            bezierPath.append(circlePath)
            bezierPath.usesEvenOddFillRule  = true
            
            let filleLayer = CAShapeLayer()
            filleLayer.path = bezierPath.cgPath
            filleLayer.fillRule = CAShapeLayerFillRule.evenOdd
            filleLayer.opacity = 0.5
            self.view.layer .addSublayer(filleLayer)
        }
    

    相关文章

      网友评论

          本文标题:绘制透明圆形遮罩层和截取圆形中的图片

          本文链接:https://www.haomeiwen.com/subject/kneegrtx.html