Print image on top of saved photo

Right now my code just takes a photo and saves it into photo gallery. I would like to have a photo be printed on top of the photo in the bottom right corner of the photo. The photo is called “logo.png”. This is just like a lower third on cable news.

import UIKit
import AVFoundation

class ViewController: UIViewController,AVCapturePhotoCaptureDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate  {

var captureSesssion : AVCaptureSession!
var cameraOutput : AVCapturePhotoOutput!
var previewLayer : AVCaptureVideoPreviewLayer!

    @IBOutlet var sa: UIView!
override func viewDidLoad() {

    captureSesssion = AVCaptureSession()
    captureSesssion.sessionPreset = AVCaptureSessionPresetPhoto
    cameraOutput = AVCapturePhotoOutput()
    let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
    if let input = try? AVCaptureDeviceInput(device: device) {
        if (captureSesssion.canAddInput(input)) {
            if (captureSesssion.canAddOutput(cameraOutput)) {
                previewLayer = AVCaptureVideoPreviewLayer(session: captureSesssion)
                    previewLayer.frame = CGRect(x: 10, y: 10, width: 700, height: 700)
@IBAction func didPressTakePhoto(_ sender: UIButton) {

          let settings = AVCapturePhotoSettings()
    let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
    let previewFormat = [
        kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
        kCVPixelBufferWidthKey as String: 160,
        kCVPixelBufferHeightKey as String: 160
    settings.previewPhotoFormat = previewFormat
    cameraOutput.capturePhoto(with: settings, delegate: self)

    func imagex(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {

    func capture(_ captureOutput: AVCapturePhotoOutput,  didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?,  previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings:  AVCaptureResolvedPhotoSettings, bracketSettings:   AVCaptureBracketedStillImageSettings?, error: Error?) {


        if  let sampleBuffer = photoSampleBuffer,
            let previewBuffer = previewPhotoSampleBuffer,
            let dataImage =  AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer:  sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {

            let dataProvider = CGDataProvider(data: dataImage as CFData)
            let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
            let image = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.right)
            UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.imagex(_:didFinishSavingWithError:contextInfo:)), nil)
            } }

Hi @zalubski,
You can load the image (the one you want to overlay as a watermark or logo) and in the capture function just before you use the UIIamgeWriteToSavedPhotos… you can from the image create a drawing context, palce the image and then save the resulting image buffer to the PhotosAlbum