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
overlayWith takes a picture as an argument and then will be overlaid on the existing picture. Position of the watermark is determined by the
image.draw which varies depending on the size of the watermark image.
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
AVCapturePhotoCaptureDelegatewhich is called right after the photos processed.
Using this, it is possible to put a watermark beautifully as in the following example.