如何在捕获图像时在相机屏幕中添加任何自定义图像?

时间:2016-06-18 05:43:21

标签: ios swift uiimagepickercontroller avcam

我想在用户捕捉照片时在相机屏幕中添加自定义UIImage。此外,我想在添加图像的相同位置上捕获的图像的前景中的图像。
Here the demo image that explain what i want
要解决这个问题,我试试

let picker = UIImagePickerController()
picker.sourceType = .Camera
picker.view.addSubView(imageView) 

但是使用此代码时,它只会添加图像而不添加带有捕获图像的图像

任何帮助都表示赞赏。

2 个答案:

答案 0 :(得分:1)

您可以使用UIImagePickerController的{​​{1}}媒体资源在相机屏幕顶部添加cameraOverlayView

imageView

要将图片添加到捕获的照片的前景,您可以为let picker = UIImagePickerController() picker.delegate = self picker.sourceType = .Camera picker.cameraOverlayView = imageView presentViewController(picker, animated: true, completion: nil) 创建扩展名:

UIImage

然后你可以从图像选择器的委托方法中调用它:

extension UIImage {
  func imageWithOverlayImage(overlayImage: UIImage) -> UIImage {
    let imageRect = CGRectMake(0, 0, self.size.width, self.size.height)

    UIGraphicsBeginImageContext(self.size)

    self.drawInRect(imageRect)
    overlayImage.drawInRect(imageRect)

    let resultImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return resultImage
  }
}

答案 1 :(得分:0)

尝试使用Overlayview。将自定义图像添加到overlayview。

func openCamera(){

        let pickercontroller : UIImagePickerController = UIImagePickerController()
        pickercontroller.delegate = self

        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){

            pickercontroller.sourceType = UIImagePickerControllerSourceType.Camera
        }
        else{
        }

        pickercontroller.editing = false
        pickercontroller.showsCameraControls = false
        let graphRect:CGRect = CGRectMake(0, 0,pickercontroller.view.frame.size.width-20 ,pickercontroller.view.frame.size.height)

        let overlayView : UIView = UIView(frame: graphRect)
        overlayView.backgroundColor = UIColor(colorLiteralRed: 0.0/255.0, green: 0/255.0, blue: 0.0/255.0, alpha: 0.5)
        overlayView.layer.opaque = false
        overlayView.opaque = false

        var maskLayer : CAShapeLayer = CAShapeLayer()
        overlayView.layer.mask = maskLayer
      }