SwiftUI | How to put a watermark in your camera app

It is common that a commercial camera app put a watermark on the photos taken from the app. It’s usually for promoting their app or inducing customers to pay for non-watermarked version. By the way, I came across a good approach to implement this thing.

How to put a watermark on any photo?

Watermark can be simply done by drawing a new image over the original image, which means we can use draw function of UIimage.

In here, overlayWith takes a picture as an argument and then will be overlaid on the existing picture. Position of the watermark is determined by the CGPoint in image.draw which varies depending on the size of the watermark image.

Left : CGPoint(x: size.width, y: size.height) / Right : CGPoint(x: size.width-#watermark size's width#, y: size.height-#watermark size's height#)

Location of watermark is determined based on size of the picture. Therefore CGPoint(x: size.width, y: size.height) is regarded as the bottom right corner.

You should know that the image used for watermark will be positioned based on its own upper left corner. That means if you set position like CGPoint(x: size.width, y: size.height), the watermark will not be visible. (See the left figure above)

To solve this, I temporarily set the location like CGPoint(x: size.width-200, y: size.height-100)and it works pretty well. If you feel uncomportable with this, add some logics subtracting size of the image from CGPoint(x: size.width, y: size.height).

How to save the watermarked photo?

You can put a watermark on your photo now. It’s time to save the photo to your album. Photos are stored in didFinishProcessingPhoto of AVCapturePhotoCaptureDelegatewhich is called right after the photos processed.

Using this, it is possible to put a watermark beautifully as in the following example.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Enebin

Enebin

I can make your dream come true — Mainly Interested in iOS / https://github.com/enebin