开发者

Slicing NSImage horizontally in cocoa

开发者 https://www.devze.com 2023-03-27 12:17 出处:网络
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.

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

}

0

精彩评论

暂无评论...
验证码 换一张
取 消