本文介绍了如何以编程方式将较大的NSImage放置在较小的NSImageView中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个100x100的NSImage。我还有一个50x50的NSImageView。有没有一种方法可以将NSImage放置在NSImageView内部的坐标处,从而可以控制显示的哪一部分?似乎NSImage没有initWithFrame方法...

Let's say I have an NSImage that's 100x100. I also have an NSImageView that's 50x50. Is there a way I can place the NSImage at coordinates inside the NSImageView, so I can control which part of it shows? It didn't seem like NSImage had an initWithFrame method...

推荐答案

我在我的NSImageView子类中做到了这一点,就像安德鲁建议的那样。

I did this in my NSImageView subclass, as Andrew suggested.

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];
    NSRect cropRect = NSMakeRect(x, y, w, h);
    [image drawAtPoint:NSZeroPoint
              fromRect:cropRect
             operation:NSCompositeCopy
              fraction:1];
}

这篇关于如何以编程方式将较大的NSImage放置在较小的NSImageView中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 22:34