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

时间:2012-09-11 12:04:04

标签: macos cocoa

我在NSImage工作我想在NSImage中创建水印我该怎么办?   任何人都可以提出建议......

1 个答案:

答案 0 :(得分: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];