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

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


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)];...


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()


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.