Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone, how does one overlay one image onto another to create a new image to save? (watermark)

Basically I want to take an image that the user chooses from their photo library and then apply a watermark, a triangle in the lower right that has the app name on it. I have the second image already made with a transparent layer in photoshop.

I tried a function, which I can't remember the exact name, but it involved CGIImages and masks. This combines the two images, but as a mask, which made the image darker where the transparent layer was and the images were not merged per se, just masked.

How would I get the watermark image to merge with another image, to make a UIImage, without displaying the images on the screen?

Thank you.

like image 358
SolidSnake4444 Avatar asked Aug 11 '11 02:08

SolidSnake4444


People also ask

How do you overlay pictures on iPhone?

Tap the photo icon at the bottom of the screen to choose a photo to superimpose. Choose a second photo that will appear over top of the first. You will now be able to move the second photo around by dragging it with your finger. You can also pinch your fingers open or closed to make your second photo bigger or smaller.

How do I add a watermark in Apple Photos?

To add a watermark you could use the Preview. app as an external editor in Photos. Drag the watermark to the position you want and adjust the size by dragging the resize corners. Now the modified image should appear in Photos.


2 Answers

It's pretty easy:

UIImage *backgroundImage = [UIImage imageNamed:@"image.png"]; UIImage *watermarkImage = [UIImage imageNamed:@"watermark.png"];  UIGraphicsBeginImageContext(backgroundImage.size); [backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)]; [watermarkImage drawInRect:CGRectMake(backgroundImage.size.width - watermarkImage.size.width, backgroundImage.size.height - watermarkImage.size.height, watermarkImage.size.width, watermarkImage.size.height)]; UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

If you want the background and watermark to be of the same size then use this code

... [backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)]; [watermarkImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)]; ... 
like image 160
omz Avatar answered Sep 20 '22 10:09

omz


The solution provided by omz also works in Swift, like so:

let backgroundImage = UIImage(named: "image.png")! let watermarkImage = UIImage(named: "watermark.png")!  UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0) backgroundImage.draw(in: CGRect(x: 0.0, y: 0.0, width: backgroundImage.size.width, height: backgroundImage.size.height)) watermarkImage.draw(in: CGRect(x: backgroundImage.size.width - watermarkImage.size.width, y: backgroundImage.size.height - watermarkImage.size.height, width: watermarkImage.size.width, height: watermarkImage.size.height)) let result = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() 
like image 21
CodeMonkey Avatar answered Sep 18 '22 10:09

CodeMonkey