设置自定义视图的背景与黑色背景的图像效果 [英] Setting the background of a custom view with an image results in a black background

查看:164
本文介绍了设置自定义视图的背景与黑色背景的图像效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题我的设置子类的NSView的背景名为 TitlebarView 。我想通过 colorWithPattternImage 使用图片作为背景,但结果却是一个黑色的背景不是我给的方法作为参数的形象。

I have a problem setting the background of my subclassed NSView called TitlebarView. I want to use an image as a background via colorWithPattternImage, but the result is a black background not the image i gave the method as a parameter.

下面是我的code TitlebarView

Here's my code of TitlebarView:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSString* pathToTitlebarBGImage = [[NSBundle mainBundle] pathForResource:@"titlebar_bg" ofType:@"png" inDirectory:@"Resources/gui_images"];
        NSLog(@"path to titlebar bg image: %@", pathToTitlebarBGImage);
        //    NSString* pathToTitlebarBGImage = @"Resources/gui_images/titlebar_bg.png";
        self.titlebarBGImage = [NSImage imageNamed:pathToTitlebarBGImage];
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
    [[NSColor colorWithPatternImage:self.titlebarBGImage] setFill];
//    [[NSColor redColor] setFill];
    NSRectFill(dirtyRect);
}

titlebarBGImage 是头文件的正确设置属性。结果
如果我用 redColor 行我得到的红色视图,因此code是工作在一定程度上。对于所有我能找到的文档的计算器上按预期这应该实际工作。缺少什么我在这里?

titlebarBGImage is a properly set property in the header files.
If I use the line with redColor I get the red colored view, so the code is working to some degree. For all I could find in the documentation an on stackoverflow this should actually work as intended. What am I missing here?

这是孤立我的整体问题的发现问题这里

This is isolated problem of my overall question found here

推荐答案

imageNamed:需要一个文件名,而不是路径名,并搜索的文件名束。如果你想从路径中使用 initWithContentsOfFile加载图像:。请参阅<一个href=\"https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSImage_Class/Reference/Reference.html\"相对=nofollow> NSImage中文档。

imageNamed: takes a file name, not a path name, and searches for the file name in the bundle. If you wish to load an image from a path use initWithContentsOfFile:. See the NSImage documentation.

这篇关于设置自定义视图的背景与黑色背景的图像效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆