面具模糊与图像

时间:2018-01-07 06:38:26

标签: ios swift uiview uivisualeffectview

因此,在我的快速应用程序中,我允许用户在手指触摸时进行绘画。我使用cgcontext来做到这一点。在用户抬起手指并且触摸结束之后,我动态地在形状顶部添加视觉效果视图,其具有与绘制形状相同的高度和宽度。

我接下来要做的是使用形状作为视觉效果视图的遮罩。现在的问题是,如果我尝试用形状掩盖visualeffect视图。除非形状的原点是(0,0),否则屏蔽视图不会显示。以下是我目前正在尝试实施此目标(https://pastebin.com/JaM9kx4G

的链接
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
}

0 个答案:

没有答案