Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging/stacking two images with Cocoa/OSX

I have a CGImageRef (lets call it original image) and a transparent png (watermark). I'm trying to write a method to place the watermark on top of the original, and return a CGImageRef.

In iOS I would have used UIKit to draw them both onto a context, but that doesn't seem possible with OSX (doesn't support UIKit).

Whats the simplest way to stack two images? Thanks

like image 633
user339946 Avatar asked Sep 03 '13 03:09

user339946


1 Answers

If anyone, like me, needs a Swift version.

This is a functional Swift 5 version:

let background = NSImage(named: "background")
let overlay = NSImage(named: "overlay")

let newImage = NSImage(size: background.size)
newImage.lockFocus()

var newImageRect: CGRect = .zero
newImageRect.size = newImage.size

background.draw(in: newImageRect)
overlay.draw(in: newImageRect)

newImage.unlockFocus()

I wish I had the time to do the same with the CGContext example.

like image 177
Fernando Mata Avatar answered Oct 05 '22 23:10

Fernando Mata