如何将图像添加到相机覆盖视图(swift4)

how to add a image to camera overlay view (swift4)

我这里的代码在拍照时在 imagePicker 的顶部显示一条红线。我想做同样的事情,但在红线的中间添加一个图像。见图。我在黄色框中添加了图像一词作为我尝试编码的示例。

    @IBAction func takePhoto(_ sender: UIButton) {
    imagePicker =  UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .camera
    present(imagePicker, animated: true, completion: nil)
    let mainView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height-150))
    let blockView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 150))
    blockView.backgroundColor = UIColor.red
    mainView.addSubview(blockView)
    imagePicker.cameraOverlayView = mainView
}

就这样:

let frame             = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: 150)
let blockView         = UIImageView.init(frame: frame)
blockView.contentMode = .scaleAspectFit
blockView.image       = UIImage(named: "NameOfYourImage")  // Just an example.

UIImageView

上面的 frame 当然需要调整,以允许您在屏幕截图中显示的顶部和底部边距。