1

I want to add custom UIImage in Camera Screen while user capture the photo. Also i want that image in foreground of that captured image on the same position the image is added.
Here the demo image that explain what i want.
To work around this i have try

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

But using this code it will only add image not add image with the capture image

Any help is appreciate.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Hi, I'm also trying similar stuff and wondering how you solve this issue. I'm using AVFoundation and want to add some UI images to both in the camera screen and the captured photo & video. Thank you in advance! – Yuuu Oct 14 '22 at 00:37
  • @Yuuu Sorry, I haven't found a proper solution for this one and not able to fix it. – Nirav D Oct 15 '22 at 06:34
  • gotcha! thank you for the message! – Yuuu Oct 17 '22 at 15:10

2 Answers2

1

You can use UIImagePickerController's cameraOverlayView property to add the imageView on top of the camera screen:

let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
picker.cameraOverlayView = imageView

presentViewController(picker, animated: true, completion: nil)

For adding the image to the foreground of the captured photo you can create an extension for 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
  }
}

Then you can call it from the delegate method of the image picker:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
  let image = info[UIImagePickerControllerOriginalImage] as! UIImage
  finalImage = image.imageWithOverlayImage(overlayImage!)
  dismissViewControllerAnimated(true, completion: nil)
}
Viktor Simkó
  • 2,607
  • 16
  • 22
0

try putting Overlayview. add custom image to 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
      }
Rushi trivedi
  • 737
  • 1
  • 15
  • 37
  • I already try with overlay view, this will only add overlay view on camera screen what i want is that dress image merge with camera image at run time when picture is click. – Nirav D Jul 18 '16 at 04:59