Questo è piuttosto semplice quando si esegue il rendering su un CGContext
.
Se si desidera un'immagine come risultato, è possibile creare e eseguire il rendering su CGBitmapContext
, quindi richiedere l'immagine dopo il rendering.
flusso generale, con i dettagli comuni e informazioni contestuale omesso:
CGImageRef CreateCompositeOfImages(CGImageRef pBackground,
const CGRect pBackgroundRect,
CGImageRef pForeground,
const CGRect pForegroundRect)
{
// configure context parameters
CGContextRef gtx = CGBitmapContextCreate(%%%);
// configure context
// configure context to render background image
// draw background image
CGContextDrawImage(gtx, pBackgroundRect, pBackground);
// configure context to render foreground image
// draw foreground image
CGContextDrawImage(gtx, pForegroundRect, pForeground);
// create result
CGImageRef result = CGBitmapContextCreateImage(gtx);
// cleanup
return result;
}
si avrebbe bisogno di creare una CGImage dal PNG.
API aggiuntive si può essere interessati a utilizzare:
CGContextSetBlendMode
CGContextSetAllowsAntialiasing
CGContextSetInterpolationQuality
.
Conosco un sacco di gente sarà generalmente consigliamo di utilizzare astrazioni di livello superiore (cioè AppKit e UIKit), ma CoreGraphics è una grande biblioteca per il rendering in entrambi i contesti. Se sei interessato alle implementazioni grafiche che sono facili da usare sia in OS X che in iOS, CoreGraphics è una buona scelta su cui basare il tuo lavoro se sei a tuo agio nel lavorare con queste astrazioni.
Nota, 'drawInRect:' è disponibile solo in 10.9 –
'[background drawAtPoint: (NSPoint) {0, 0} fromRect: newImageRect operazione: NSCompositeSourceOver fraction: 1.0];' funziona per pre 10.9 –