2011-08-12 43 views

回答

3

这是我刚添加到我的工具箱中的一个技巧。我已经将它添加为NSImage上的一个类别。您传入源图像和从中切出新图像的矩形。下面的代码:

+ (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

非常感谢。谢谢。 – Soorya

+0

@Soorya如果这个答案对您有帮助,请点击勾号将其标记为已接受 –