Cocoa 图像位于其他视图上方的滚动视图图形中

Cocoa 图像位于其他视图上方的滚动视图图形中,cocoa,drawing,core-graphics,nsimage,nsscrollview,Cocoa,Drawing,Core Graphics,Nsimage,Nsscrollview,我有一个自定义NSView,它位于NSScrollView中的NSSplitView中。自定义视图使用以下图形代码: - (void)drawRect:(NSRect)dirtyRect { NSGraphicsContext *ctx = [NSGraphicsContext currentContext]; [ctx saveGraphicsState]; // Rounded Rect NSRect rect = [self bounds]; NS

我有一个自定义NSView,它位于
NSScrollView
中的
NSSplitView
中。自定义视图使用以下图形代码:

- (void)drawRect:(NSRect)dirtyRect {
    NSGraphicsContext *ctx = [NSGraphicsContext currentContext];
    [ctx saveGraphicsState];

    // Rounded Rect
    NSRect rect = [self bounds];
    NSRect pathRect = NSMakeRect(rect.origin.x + 3, rect.origin.y + 6, rect.size.width - 6, rect.size.height - 6);
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:pathRect cornerRadius:kDefaultCornerRadius];

    // Shadow
    [NSShadow setShadowWithColor:[NSColor colorWithCalibratedWhite:0 alpha:0.66]
                      blurRadius:4.0
                          offset:NSMakeSize(0, -3)];     
    [[NSColor colorWithCalibratedWhite:0.196 alpha:1.0] set];
    [path fill];
    [NSShadow clearShadow];


    // Background Gradient
    NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:[UAColor darkBlackColor] endingColor:[UAColor lightBlackColor]];
    [gradient drawInBezierPath:path angle:90.0];
    [gradient release];


    // Image
    [path setClip];
    NSRect imageRect = NSMakeRect(pathRect.origin.x, pathRect.origin.y, pathRect.size.height * kLargeImageRatio, pathRect.size.height);
    [self.image drawInRect:imageRect
                  fromRect:NSZeroRect
                 operation:NSCompositeSourceAtop
                  fraction:1.0];

    [ctx restoreGraphicsState];

    [super drawRect:dirtyRect];
}
我尝试了各种不同类型的
操作
,但图像仍然绘制在
NSSplitView
的另一半上方,如下所示:


…而不是在
NSScrollView
下绘图。我认为这与绘制所有内容有关,而不是仅绘制
dirtyRect
,但我不知道如何编辑图像绘制代码,以仅绘制位于
dirtyRect
中的部分如何防止它在顶部绘制,或仅绘制此NSImage的脏矩形?

我终于得到了它。我不知道它是否是最优的,但我会在进行性能测试时发现。我只需要使用
NSIntersectionRect
计算出图像rect和脏rect的交集,然后计算出为
drawInRect:fromRect:operation:fraction:
调用绘制
NSImage
的哪个子部分

以下是重要部分:

    NSRect imageRect = NSMakeRect(pathRect.origin.x, pathRect.origin.y, pathRect.size.height * kLargeImageRatio, pathRect.size.height);
    [self.image setSize:imageRect.size];

    NSRect intersectionRect = NSIntersectionRect(dirtyRect, imageRect);
    NSRect fromRect = NSMakeRect(intersectionRect.origin.x - imageRect.origin.x,
                                 intersectionRect.origin.y - imageRect.origin.y,
                                 intersectionRect.size.width,
                                 intersectionRect.size.height);
    [self.image drawInRect:intersectionRect
                  fromRect:fromRect
                 operation:NSCompositeSourceOver
                  fraction:1.0];