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

时间:2017-12-08 15:13:02

标签: ios uiimagepickercontroller swift4 camera-overlay cameraoverlayview

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

    @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
}

enter image description here

1 个答案:

答案 0 :(得分:0)

只是做:

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当然需要调整,以允许您在屏幕截图中显示的上边距和下边距。