2011-08-12 48 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如果這個答案對您有幫助,請點擊勾號將其標記爲已接受 –