Soorya
Soorya

Reputation: 41

Slicing NSImage horizontally in cocoa

I need to slice an NSImage into two equal halves horizontally. Please help me out. Thanks in advance.

Upvotes: 0

Views: 592

Answers (1)

Francis McGrew
Francis McGrew

Reputation: 7272

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];    

}

Upvotes: 3

Related Questions