-1

before i post some code and say some things, i'm using this pod for taking photos and videos : https://github.com/imaginary-cloud/CameraManager What i want to do is simply take the captured image, put a text over it at the bottom (that text can be picked via an existing array of phrases that i already created) and i want to merge this two entities into one new image, that it will be displayed in a new view controller and saved in the camera roll, after the original of course. How can i do that? Thanks

Here's my capturePhoto code:

let when = DispatchTime.now() + 5
    func capturePicture(){
        _ = cameraManager.addPreviewLayerToView(cameraPreview, newCameraOutputMode: CameraOutputMode.stillImage)

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
            // do stuff x seconds later

            self.cameraManager.capturePictureWithCompletion({ (image, error) -> Void in

                self.cameraPreview.image = image

            })

        }


    }

2 Answers2

0

Try the code snippet below

UIGraphicsBeginImageContextWithOptions(image!.size, false, 0.0)
// I tought cameraPreview is an imageview
cameraPreview.layer.render(in: UIGraphicsGetCurrentContext()!)
YOUR_UILABEL.layer.render(in: UIGraphicsGetCurrentContext()!)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
let imageData = UIImageJPEGRepresentation(newImage!, 0.9)
let pngImage = UIImage(data: imageData!)
UIGraphicsEndImageContext()

Then your new image object is ready on pngImage

And the protocol sample is below

// you can add this protocol to first viewcontroller
protocol ReceiveImageProtocol: class {

    func onReceiveImage(image: UIImage)

}

class FirstViewController: UIViewController, ReceiveImageProtocol {

    ...

    internal func onReceiveImage(image: UIImage) {
        // use merger function for image and label
    }

    internal func openSeconViewController() {
        let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
        secondViewController.imageProtocol = self
        // push or present secondViewController
    }
}

class SecondViewController: UIViewController {

    public weak var imageProtocol: ReceiveImageProtocol!

    // when you capture return back to firstviewcontroller
    // using pop or dismiss
    // and don't forget to call
    // self.imageProtocol.onReceiveImage(image: YOUR_CAPTURED_IMAGE)
}
abdullahselek
  • 7,893
  • 3
  • 50
  • 40
  • Thanks, will try implementing it, i'll report back if it worked/didn't work. Thanks for now ! – Mattia Gerardi Mar 09 '17 at 10:19
  • My friend, i have a problem, i didn't write this detail in the original question, but my random label is in another viewcontroller, how can i call it in my master one, the one where the function above is located? – Mattia Gerardi Mar 09 '17 at 14:02
  • If you capture just one image you can send this image via a delegate to previous viewcontroller, or for multiple images send the label from first viewcontroller to capture image viewcontroller when you push or present the capture image viewcontroller. – abdullahselek Mar 09 '17 at 16:27
  • I'm still a beginner/intermediate in xcode development, can you do me a favor and explain it to me via an code example? So i try to implement it – Mattia Gerardi Mar 09 '17 at 16:29
  • I have just added a protocol sample to my answer, from now on thats all yours :) – abdullahselek Mar 09 '17 at 16:59
  • Don't forget to accept my answers ;) – abdullahselek Mar 09 '17 at 17:06
  • Thanks man, you really helped me here, without assaulting me when i asked for an example. I'll try this tomorrow asap! – Mattia Gerardi Mar 09 '17 at 20:39
0

You can do this by using UIGraphicsBeginImageContextWithOptions.

UIGraphicsBeginImageContextWithOptions(self.cameraPreview.bounds.size, false, UIScreen.main.scale) self.imageView.layer.render(in: UIGraphicsGetCurrentContext()!)
let captureImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

And you get captured image with text over it.

Daggarwal
  • 234
  • 1
  • 7