如何在Mac应用程序中创建NSImage水印

3

我正在使用NSImage,我想在NSImage中创建水印,如何做到呢?请问有没有建议可以帮助我。

1个回答

2

类似这样的:

float width = 10.0;
float height = 10.0;

NSImage *finalImage = [[NSImage alloc] initWithSize:NSMakeSize(width, height)];

//  obtain images - your sources may vary
NSImage *overlay = [[NSImage alloc] initWithData:[NSData dataWithContentsOfFile:@"/path/to/overlay_image.jpg"]];
NSImage *mainImage = [[NSImage alloc] initWithData:[NSData dataWithContentsOfFile:@"/path/to/main_image.jpg"]];

[finalImage lockFocus];

// draw the base image
[mainImage drawInRect:NSMakeRect(0, 0, width, height) 
                      fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

// draw the overlay image at some offset point
[overlay drawInRect:NSMakeRect(10, 10, [overlay size].width, [overlay size].height) 
             fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

[finalImage unlockFocus];

NSData *finalData = [finalImage TIFFRepresentation];

[[[NSBitmapImageRep imageRepWithData:finalData] representationUsingType:NSJPEGFileType properties:nil] writeToFile:[NSString stringWithFormat:@"/path/to/folder/new_image.jpg"] atomically:YES];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接