Slicing NSImage horizontally in cocoa
I need to slice an NSImage into two equal halves horiz开发者_高级运维ontally. Please help me out. Thanks in advance.
This is a trick I just added to my toolkit. I've added it as a category on NSImage. You pass in the source image and the rect from which to slice a new image. Here's the code:
+ (NSImage *) sliceImage:(NSImage *)image fromRect:(NSRect)srcRect {
NSRect targetRect = NSMakeRect(0, 0, srcRect.size.width, srcRect.size.height);
NSImage *result = [[NSImage alloc] initWithSize:targetRect.size];
[result lockFocus];
[image drawInRect:targetRect fromRect:srcRect operation:NSCompositeCopy fraction:1.0];
[result unlockFocus];
return [result autorelease];
}
精彩评论