NSScrollview中的NSView。文档视图滚动的问题

时间:2014-03-31 14:32:00

标签: nsview nsscrollview

我有一个NSView子类ImageTransform,我使用Core Image过滤器CILanczosScaleTransform执行图像缩放。然后将图像绘制到表面。一切都好,直到这一点。 我想将NSView子类(ImageTransform)嵌入到NSScrollView中,以便在视图对象大于Window的内容大小时执行滚动。

因此我使用以下代码实现滚动视图:

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

    ImageTransform *imageTransform = [[ImageTransform alloc]init];
    [imageTransform doZooming];
    [self.window setContentSize:NSMakeSize(800, 600)];


    //Get width and height of the image after zooming and set the ImageTransform     instance bounds to match output image dimensions.
    float height = imageTransform.imgOutHeight;
    float width = imageTransform.imgOutWidth;
    [imageTransform setFrame:NSMakeRect(0, 0, width, height)];

    //Set up a scroll view
    self.scrollView = [[NSScrollView alloc]initWithFrame:[[self.window contentView]frame]];
    [self.scrollView setHasVerticalScroller:YES];
    [self.scrollView setHasHorizontalScroller:YES];
    [self.scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
    [self.scrollView setBorderType:NSNoBorder];
    [self.scrollView setDocumentView:imageTransform];
    [self.window setContentView:self.scrollView];
    NSLog(@"NSView width %f height %f", imageTransform.bounds.size.width, imageTransform.bounds.size.height);

}
@end

然而,问题是ScrollViewer的文档视图(它是ImageTransform类的一个实例,并且比self.window的内容视图大)填充进入contentView 。当我移动鼠标时图像会四处移动,但是当它移动时,它会变形并变得模糊。当我创建一个NSImageView对象并将其作为参数传递给scrollview时,设置滚动视图很顺利,但在我的情况下,我必须使用imageTransform作为文档视图。

我知道这是一个新手问题而且我道歉(因为我在这里是新手:)。设置scrollview应该很容易,但到目前为止我已经尝试过所有内容并且没有运气。

我也在添加ImageTransform类代码(这是调整图像大小和绘图的地方):

-(CIImage*)doZooming
{

    NSURL *imageURL = [[NSBundle mainBundle]URLForResource:@"the-shining-axe" withExtension:@"jpg"];
    CIImage *inputImage = [CIImage imageWithContentsOfURL:imageURL];
    self.transformFilter = [CIFilter filterWithName:@"CILanczosScaleTransform"];
    [self.transformFilter setValue:[NSNumber numberWithFloat:2.0] forKey:@"inputScale"];
    [self.transformFilter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputAspectRatio"];
    [self.transformFilter setValue:inputImage forKey:@"inputImage"];
    self.outputImage = [self.transformFilter valueForKey:@"outputImage"];
    self.outputImageExtent = self.outputImage.extent;
    self.imgOutHeight = self.outputImageExtent.size.height;
    self.imgOutWidth = self.outputImageExtent.size.width;
    return self.outputImage;
}


- (void)drawRect:(NSRect)dirtyRect
{
    //[super drawRect:dirtyRect];

    self.cIContext = [[NSGraphicsContext currentContext] CIContext];

    [self.cIContext drawImage:[self doZooming] inRect:dirtyRect fromRect:self.outputImage.extent];
}

@end

1 个答案:

答案 0 :(得分:1)

我设法通过更改以下代码行来解决此问题: [self.cIContext drawImage:[self doZooming] inRect:dirtyRect fromRect:self.outputImage.extent];

到此:

[self.cIContext drawImage:[self doZooming] inRect:self.outputImage.extent fromRect:self.outputImage.extent];