我有一個顯示圖像的NSView,我想使這個視圖的行爲像一個裁剪圖像效果。然後,我製作了3個矩形(imageRect
,secRect
和IntersectRect
),imageRect
是顯示圖像的矩形,secRect
是矩形,它只是使整個圖像變暗,而intersectRect
是一個矩形,它像一個觀察矩形,我想要的做就像在secRect
上做個「洞」,直接看到imageRect
(沒有變暗)。這裏是我的drawRect方法:使用NSImage操作產生作物效果
- (void)drawRect:(NSRect)rect {
// Drawing code here.
NSImage *image = [NSImage imageNamed:@"Lonely_Tree_by_sican.jpg"];
NSRect imageRect = [self bounds];
[image compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver ];
if (NSIntersectsRect([myDrawRect currentRect], [self bounds])) {
//get the intersectionRect
intersectionRect = NSIntersectionRect([myDrawRect currentRect], imageRect);
//draw the imageRect
[image compositeToPoint:imageRect.origin operation:NSCompositeSourceOver];
//draw the secRect and fill it with black and alpha 0.5
NSRect secRect = NSMakeRect(imageRect.origin.x, imageRect.origin.y, imageRect.size.width, imageRect.size.height);
[[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:0.5] set];
[NSBezierPath fillRect:secRect];
//have no idea for the intersectRect
/*[image compositeToPoint:intersectionRect.origin
fromRect:secLayer
operation:NSCompositeXOR
fraction:1.0];*/
}
//draw the rectangle
[myDrawRect beginDrawing];
}
我有我自己的類(myDrawRect)來繪製基於鼠標點擊[self bounds]
矩形,因此忽略了beginDrawing
命令。
任何幫助會很好,謝謝。赫布。