Swift 使用绘制的图像遮罩VisualEffect模糊
因此,在我的swift应用程序中,我允许用户用手指触摸画画。我正在使用cgcontext来实现这一点。在用户抬起手指并触摸结束后,我将在形状顶部动态添加一个visualeffect视图,该视图的高度和宽度与绘制的形状相同。我接下来要做的是将该形状用作visualeffect视图的遮罩。现在的问题是,如果我试图用形状遮罩visualeffect视图。除非形状的原点为(0,0),否则遮罩视图不会显示。这里有一个链接,指向我目前如何尝试实现此()Swift 使用绘制的图像遮罩VisualEffect模糊,swift,mask,cgcontext,uivisualeffectview,Swift,Mask,Cgcontext,Uivisualeffectview,因此,在我的swift应用程序中,我允许用户用手指触摸画画。我正在使用cgcontext来实现这一点。在用户抬起手指并触摸结束后,我将在形状顶部动态添加一个visualeffect视图,该视图的高度和宽度与绘制的形状相同。我接下来要做的是将该形状用作visualeffect视图的遮罩。现在的问题是,如果我试图用形状遮罩visualeffect视图。除非形状的原点为(0,0),否则遮罩视图不会显示。这里有一个链接,指向我目前如何尝试实现此() override func touchsbegind(
override func touchsbegind(touch:Set,带有事件:UIEvent?){
刷卡=错误
如果让触摸=触摸。首先作为?UITouch{
如果(touch.view==侧视图){
返回
}
tempPath=UIBezierPath()
lastPoint=触摸位置(在:视图中)
tempPath.move(到:最后一点)
}
}
func drawLineFrom(起点:CGPoint,地形点:CGPoint){
addLine(to:CGPoint(x:toPoint.x,y:toPoint.y))
UIGraphicsBeginImageContext(view.frame.size)
let context=UIGraphicsGetCurrentContext()
otherImageView.image?.draw(in:CGRect(x:0,y:0,width:view.frame.size.width,height:view.frame.size.height))
// 2
上下文!.move(移动到:CGPoint(x:fromPoint.x,y:fromPoint.y))
context!.addLine(到:CGPoint(x:toPoint.x,y:toPoint.y))
// 3
context!.setLineCap(CGLineCap.round)
上下文!.setLineWidth(笔刷宽度)
context!.setStrokeColor(红色:红色,绿色:绿色,蓝色:蓝色,alpha:1.0)
上下文模式(CGBlendMode.normal)
// 4
上下文!.strokePath()
// 5
otherImageView.image=UIGraphicsGetImageFromCurrentImageContext()
otherImageView.alpha=不透明度
UIGraphicsSendImageContext()
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
刷卡=正确
如果让触摸=触摸。首先作为?UITouch{
让currentPoint=触摸位置(在:视图中)
drawLineFrom(起点:最后一点,拓扑点:当前点)
// 7
lastPoint=currentPoint
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
tempPath.close()
设tempImage=UIImageView(帧:cRect(x:0,y:0,width:view.frame.size.width,height:view.frame.size.height))
UIGraphicsBeginImageContext(tempImage.frame.size)
tempImage.image?.draw(in:CGRect(x:0,y:0,width:view.frame.size.width,height:view.frame.size.height),blendMode:CGBlendMode.normal,alpha:1.0)
otherImageView.image?.draw(in:CGRect(x:0,y:0,宽度:view.frame.size.width,高度:view.frame.size.height),blendMode:CGBlendMode.normal,alpha:opacity)
let context=UIGraphicsGetCurrentContext()
让image=UIGraphicsGetImageFromCurrentImageContext()
tempImage.image=image
otherImageView.image=nil
imageView.addSubview(tempImage)
让模糊=VisualEffectView()
blur.frame=CGRect(x:tempPath.bounds.origin.x,y:tempPath.bounds.origin.y,宽度:tempPath.bounds.width,高度:tempPath.bounds.height)
blur.blurRadius=5
blur.layer.mask=tempImage.layer
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
swiped = false
if let touch = touches.first as? UITouch {
if (touch.view == sideView){
return
}
tempPath = UIBezierPath()
lastPoint = touch.location(in: view)
tempPath.move(to:lastPoint)
}
}
func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
tempPath.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))
UIGraphicsBeginImageContext(view.frame.size)
let context = UIGraphicsGetCurrentContext()
otherImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
// 2
context!.move(to: CGPoint(x:fromPoint.x,y:fromPoint.y))
context!.addLine(to: CGPoint(x:toPoint.x, y:toPoint.y))
// 3
context!.setLineCap(CGLineCap.round)
context!.setLineWidth(brushWidth)
context!.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
context!.setBlendMode(CGBlendMode.normal)
// 4
context!.strokePath()
// 5
otherImageView.image = UIGraphicsGetImageFromCurrentImageContext()
otherImageView.alpha = opacity
UIGraphicsEndImageContext()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
swiped = true
if let touch = touches.first as? UITouch {
let currentPoint = touch.location(in: view)
drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)
// 7
lastPoint = currentPoint
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
tempPath.close()
let tempImage = UIImageView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
UIGraphicsBeginImageContext(tempImage.frame.size)
tempImage.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: CGBlendMode.normal, alpha: 1.0)
otherImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: CGBlendMode.normal, alpha: opacity)
let context = UIGraphicsGetCurrentContext()
let image = UIGraphicsGetImageFromCurrentImageContext()
tempImage.image = image
otherImageView.image = nil
imageView.addSubview(tempImage)
let blur = VisualEffectView()
blur.frame = CGRect(x:tempPath.bounds.origin.x,y:tempPath.bounds.origin.y, width:tempPath.bounds.width, height:tempPath.bounds.height)
blur.blurRadius = 5
blur.layer.mask = tempImage.layer
}