相机叠加Swift上不会出现绘制的图像

时间:2017-06-28 07:06:53

标签: ios swift xcode uiimagepickercontroller

我正在尝试在拍摄照片后创建一个位于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
}

1 个答案:

答案 0 :(得分:0)

这只是猜测,但值得一试。我有一个类似的问题,但在不同的背景下......我正在使用AVPlayer。

就我而言,因为zPosition没有正确设置,所以我试图绘制的叠加层出现在视频背后。假设情况如此,修复方法是设置zPosition = -1,将其进一步向后移动:

AVPlayerView.layer?.zPosition = -1

这会移动叠加层后面的基础层(负数在3D空间中远离用户移动。)

我会看到你的UIImagePickerController控制的视图是否允许你设置它类似于AVPlayer允许的zPosition。如果我没记错的话,任何视图都应该能够设置它的zPosition。或者,您可以尝试将叠加层移动到正值,例如zPosition = 1.(我不确定,但我认为默认位置为0。)

相关问题