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

5

28

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.

Cheerio answered 11/8, 2011 at 2:10 Comment(1)
possible duplicate of Create a UIImage from two other UIImages on the iPhoneUnfold
G
83

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)];
...
Gambeson answered 11/8, 2011 at 2:28 Comment(6)
Worked perfectly! You're right, that was pretty easy. Thank you very much for the help!Cheerio
It's better to use UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext to preserve image quality on retina displays. See #4334733 for more info.Pneumonoultramicroscopicsilicovolcanoconiosis
You sir deserve cake! This is an excellent solution. I have the same need and you have saved me a lot of time.Inpatient
Why does this not preserve retina image resolution? Any advice how to retain it?Lollapalooza
This is how to retain retina resolution: #4334733Lollapalooza
If background image is small then watermark is showing very large. Please provide any advice.Optics
S
13

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()
Scup answered 16/6, 2015 at 10:10 Comment(0)
L
2

SWIFT 4

let backgroundImage = imageData!
let watermarkImage = #imageLiteral(resourceName: "jodi_url_icon")

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: 10, y: 10, width: watermarkImage.size.width, height: backgroundImage.size.height - 40))

let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.imgaeView.image = result

Use result to UIImageView, tested.

Leucoderma answered 18/12, 2018 at 11:29 Comment(0)
T
1

You can use this method, which is very dynamic and you can specify the starting position of the second image and total size of the image.

-(UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2 andRect:(CGRect)cropRect withImageWidth:(int) width{

    CGSize size = CGSizeMake(width,40);
    UIGraphicsBeginImageContext(size);

    CGPoint pointImg1 = CGPointMake(0,0);
    [img drawAtPoint:pointImg1];

    CGPoint pointImg2 = cropRect.origin;
    [img2 drawAtPoint: pointImg2];

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;

}
Torpid answered 12/8, 2013 at 9:44 Comment(0)
M
1

SWIFT 5 Function:

func addWaterMark(image: UIImage) -> UIImage {
        let backgroundImage = image//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()
        return result!
    }
Michamichael answered 13/4, 2020 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.