如何将NSImage绘制到NSView上?

3

我对使用 Objective C 进行图像绘制还很陌生。在 iPhone 开发中,我已经完成了图像的绘制,但现在我想在 Mac 上进行。简而言之,下面的 iPhone 代码在 Mac 上有什么等效的实现方式?

- (void) drawRect: (CGRect) rect
{
    [super drawRect:rect];

    UIImage *anotherimage = [UIImage imageNamed:@"alert.png"];
    CGPoint imagepoint = CGPointMake(10,0);
    [anotherimage drawAtPoint:imagepoint];

}
1个回答

3

假设您不需要任何图像透明度和组合效果,那么这应该可以工作。

-(void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
    NSImage *anotherImage = [NSImage imageNamed:@"alert.png"];
    [anotherImage drawAtPoint:NSMakePoint(10,0) fromRect:rectToDrawImage operation:NSCompositeCopy fraction:1.0];
}

1
你可能已经知道了,但是你可能想把图像的创建移出 drawRect: 方法中,因为该方法必须尽可能保持简洁和快速。 - lead_the_zeppelin
1
非常感谢。我需要做一些更改,但最终它起作用了。我添加了NSMakeRect(0,0,[anotherImage size].width, [anotherImage size].height)。 - Scsm326
NSImage没有“frame”属性,就像Xcode所说的那样。 - Will

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接