2014-03-31 49 views
1

我有一個NSView的子類ImageTransform,我通過使用核心圖像過濾器CILanczosScaleTransform執行圖像縮放。然後圖像被繪製到表面。一切都好,直到這一點。 我想將NSView子類(ImageTransform)嵌入到NSScrollView中,以便當視圖對象比窗口的內容大小大時,它執行滾動。NSScrollview中的NSView。文檔視圖滾動的問題

爲此,我實現用下面的代碼滾動視圖:

@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類的實例,這是比自己大.window的contentView)填入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

我管理通過改變下面的代碼行,以解決這個問題: [self.cIContext drawImage:[self doZooming] inRect:dirtyRect fromRect:self.outputImage.extent];

這樣:

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