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

/// 截取遮罩层中镂空部分的内容生成图片
       @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)
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容