首页 文章

NSColor colorWithPatternImage:Vs. NSImage drawInRect:fromRect:operation:fraction:

提问于
浏览
0

所以我试图将视图的背景设置为图像,并且我在另一个线程中读取,为了避免重复图像,应该使用 NSImage drawInRect:fromRect:operation:fraction: 而不是 NSColor colorWithPatternImage: 但是,当我使用前者时,图像不需要't draw in the view, while it works with the latter (but repeats, which I don't) . 这里有什么我想念的吗?该图像位于Images.xcassets文件夹中,我按照其他线程中的建议实现了这两种方法:How to make NSView's background image not repeat?

这是我的代码:

#import "OrangeBGView.h"

@implementation OrangeBGView

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor blueColor] setFill];//To easily see if image isn't loading
    NSRectFill(dirtyRect);

    [[NSColor colorWithPatternImage:[NSImage imageNamed:@"orangeGradientBGWithTopEdgeShadow.png"]] setFill];//Working
//    [[NSImage imageNamed:@"orangeGradientBGWithTopEdgeShadow.png"] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];//Not working
    NSRectFill(dirtyRect);

}

@end

1 回答

  • 0

    这对我来说是一个简单的疏忽 . drawInRect:fromRect:operation:fraction: 工作正常,但我留在了最后的 NSRectFill(dirtyRect) 线,它基本上画了我正在绘制的图像 . 此代码现在有效:

    #import "OrangeBGView.h"
    
    @implementation OrangeBGView
    
    - (void)drawRect:(NSRect)dirtyRect
    {
        [[NSColor blueColor] setFill];//To easily see if image isn't loading
        NSRectFill(dirtyRect);
    
        [[NSImage imageNamed:@"orangeGradientBGWithTopEdgeShadow.png"] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
    }
    
    @end
    

相关问题