ios - 绘制的图像不会出现在相机覆盖 Swift 上

标签 ios swift xcode uiimagepickercontroller

我正在尝试创建一个绘图应用程序,该应用程序在拍摄照片后位于 UIImagePickerController 之上。触摸委托(delegate)函数被正确调用,但不会像预期那样在 View 上留下任何痕迹。到目前为止我所拥有的:

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 控制的 View 是否允许你设置它的 zPosition 类似于 AVPlayer 允许的。如果我没记错的话,任何 View 都应该能够设置它的 zPosition。或者,您可以尝试将覆盖层移动到正值,例如 zPosition = 1。(我不确定,但我认为默认位置是 0。)

关于ios - 绘制的图像不会出现在相机覆盖 Swift 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44795754/

相关文章:

ios - iPhone 模拟器屏幕分辨率 vs iPhone 屏幕分辨率

来自字符串的 iOS NSDate 只有时间

ios - 在在线游戏服务器上验证 Game Center 玩家

ios - 无法创建 NSManagedObject 子类

ios - 在SKPSMTPMessage中附加视频

iphone - 在 iPad 中一次轻按两个按钮

javascript - WKScriptMessageHandler 不会在网页上的按钮元素上监听 'onclick' 或 'click' 事件。网页使用Reactjs开发

swift - 迭代PHAsset以获取图像标识符Swift

ios - ReachabilitySwift pod 在 Swift 2.3 中导致错误

ios - 单击警报按钮不起作用时更改 View