본문 바로가기

개발관련/Mac

NSView 배경 이미지 지정


// header

NSImage *backgroundImg;

- (id) initWithFrame:(NSRect)frameRect
{
    if ( ( self = [super initWithFrame:framerect] ) != nil ) {
        NSString *szImgName = [[NSBundle mainBundle] pathForResource:@"img" 
            ofType:@"png"];

        backgroundImg = [[NSImage alloc] initWithContentsOfIle:szImgName]; 
    }

    return self;
} 

- (void) dealloc
{
    [backgroundImg release];

    [super dealloc];
}

- (void) drawRect:(NSRect)rect
{
    NSRect imgRect;
    NSRect drawRect;

    imgRect.origin = NSZeroPoint;
    imgRect.size = [backgroundImg size];

    drawRect = [self bounds];

    [backgroundImg drawInRect:drawRect
                            fromRect:imgRect
                            operation:NSCompositeSourceOver
                            fraction:1.0];
}