绘制透明圆形遮罩层和截取圆形中的图片
2022-08-18 本文已影响0人
leafu
/// 截取遮罩层中镂空部分的内容生成图片
@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)
}