绘制的图像不会出现在摄像机覆盖swift上



我正在尝试创建一个绘制应用程序,该应用位于图片后位于UIImagePickerController之上。触摸委托函数被正确调用,但不要在视图上留下任何痕迹。到目前为止我拥有的东西:

func createImageOptionsView() { //creates the view above the picker controller overlay view
    let imgOptsView = UIView()
    let scale = CGAffineTransform(scaleX: 4.0 / 3.0, y: 4.0 / 3.0)
    imgOptsView.tag = 1
    imgOptsView.frame = view.frame
    let imageView = UIImageView()
    imageView.frame = imgOptsView.frame
    imageView.translatesAutoresizingMaskIntoConstraints = true
    imageView.transform = scale //to account for the removal of the black bar in the camera
    let useButton = UIButton()
    imageView.tag = 2
    imageView.contentMode = .scaleAspectFit
    imageView.image = currentImage
    useButton.setImage(#imageLiteral(resourceName: "check circle"), for: .normal)
    useButton.translatesAutoresizingMaskIntoConstraints = true
    useButton.frame = CGRect(x: view.frame.width / 2, y: view.frame.height / 2, width: 100, height: 100)
    let cancelButton = UIButton()
    colorSlider.previewEnabled = true
    colorSlider.translatesAutoresizingMaskIntoConstraints = true
    imgOptsView.addSubview(imageView)
    imgOptsView.addSubview(useButton)
    imgOptsView.addSubview(colorSlider)
    imgOptsView.isUserInteractionEnabled = true
    useButton.addTarget(self, action: #selector(ViewController.usePicture(sender:)), for: .touchUpInside)
    picker.cameraOverlayView!.addSubview(imgOptsView)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    mouseSwiped = false //to check if the touch moved or was simply dotted
    let touch: UITouch? = touches.first
    lastPoint = touch?.location(in: view) //where to start image context from
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    mouseSwiped = true
    let touch: UITouch? = touches.first
    let currentPoint: CGPoint? = touch?.location(in: view)
    UIGraphicsBeginImageContext(view.frame.size) //begin drawing
    tempDrawImage.image?.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(view.frame.size.width), height: CGFloat(view.frame.size.height)))
    UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: CGFloat(lastPoint.x), y: CGFloat(lastPoint.y)))
    UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: CGFloat((currentPoint?.x)!), y: CGFloat((currentPoint?.y)!)))
    UIGraphicsGetCurrentContext()!.setLineCap(.round)
    UIGraphicsGetCurrentContext()?.setLineWidth(1.0)
    UIGraphicsGetCurrentContext()?.setStrokeColor(c)
    UIGraphicsGetCurrentContext()!.setBlendMode(.normal)
    UIGraphicsGetCurrentContext()?.strokePath() //move from lastPoint to currentPoint with these options
    tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    lastPoint = currentPoint
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if !mouseSwiped {
        UIGraphicsBeginImageContext(view.frame.size)
        tempDrawImage.image?.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(view.frame.size.width), height: CGFloat(view.frame.size.height)))
        UIGraphicsGetCurrentContext()!.setLineCap(.round)
        UIGraphicsGetCurrentContext()?.setLineWidth(1.0)
        UIGraphicsGetCurrentContext()?.setStrokeColor(c)
        UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: CGFloat(lastPoint.x), y: CGFloat(lastPoint.y)))
        UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: CGFloat(lastPoint.x), y: CGFloat(lastPoint.y)))
        UIGraphicsGetCurrentContext()?.strokePath()
        UIGraphicsGetCurrentContext()!.flush()
        tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    UIGraphicsBeginImageContext(mainImage.frame.size)
    mainImage.image?.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(view.frame.size.width), height: CGFloat(view.frame.size.height)), blendMode: .normal, alpha: 1.0)
    tempDrawImage.setNeedsDisplay()
    mainImage.image = UIGraphicsGetImageFromCurrentImageContext() //paste tempImage onto the main image and then start over
    tempDrawImage.image = nil
    UIGraphicsEndImageContext()
}
func changedColor(_ slider: ColorSlider) {
    c = slider.color.cgColor
}

这只是一个猜测,但值得一试。我有一个类似的问题,但在不同的情况下...发生时我正在与Avplayer合作。

就我而言,这是因为zposition无法正确设置,所以我试图绘制的叠加层出现在视频后面。假设是这样,修复程序将是设置zposition = -1,它像这样将其移动到更进一点:

AVPlayerView.layer?.zPosition = -1

这移动了覆盖层后面的基层(负数在3D空间中远离用户。(

我会看看您的uiimagePickerController控制的视图是否允许您设置它的Zposition类似于Avplayer允许的范围。如果我没记错的话,任何视图都应该能够将其设置为zposition。另外,您可以尝试将覆盖层移至Zposition = 1之类的正值。(我不确定,但是我想默认值是位置为0。(

最新更新