IBG
IBG

Reputation: 465

Overlay an image (UIImageView) into photo

Is there a way to integrate a UIImageView when taking photos?

I can't seem to find a topic discussing this.

What I wanted to do is to have a UIImageView overlay on the preview. When the shutter is tapped, it should take the picture with the UIImageView embedded in it.

enter image description here

So here's where I am right now. The "test image" part will be made draggable on top of the preview view. I want to include that image with the image I'll be taking when I push the shutter button.

Is this possible? Or is there a better way?

I'm using AVCaptureVideoPreviewLayer, AVCaptureSession and AVCapturePhotoOutput. I'm just putting the UIIMageView above the UIView where the AVCaptureVideoPreviewLayer is also a subview.

EDIT: Tested saving the UIView as an image, and it doesn't include the preview layer (only returns the UIView + UIImageView overlay).

Upvotes: 1

Views: 452

Answers (1)

Sergey Kuryanov
Sergey Kuryanov

Reputation: 6124

There is at least two ways of doing this.

1) Capture photo from camera and add overlay image based on draggable image view position. This will produce best possible quality.

2) If you don't need photo quality you can use another solution - render draggable view layer in graphic context and video preview layer:

func takePhotoWithOverlay() -> UIImage? {
    let overlayImageView // UIImageView with overlay image
    let videoPreviewLayer // AVCaptureVideoPreviewLayer

    guard let superview = overlayImageView.superview else { return nil }

    UIGraphicsBeginImageContextWithOptions(superview.bounds.size, false, 0.0)
    defer { UIGraphicsEndImageContext() }

    guard let context = UIGraphicsGetCurrentContext() else { return nil }

    videoPreviewLayer.render(in: context)
    overlayImageView.layer.render(in: context)

    return UIGraphicsGetImageFromCurrentImageContext()
}

Upvotes: 2

Related Questions