Reputation: 21
I have a NSView subclass ImageTransform where I perform image zooming by using Core Image filter CILanczosScaleTransform. Image then is drawn to the surface. All ok till this point. I would like to embed the NSView subclass (ImageTransform) into NSScrollView so that it performs scrolling when the view object gets bigger than Window's content size.
For this reason I implement the scroll view with the following code:
@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
However, the problem is that the ScrollViewer's document view (which is an instance of ImageTransform class and which is bigger than self.window's contentView) fills into the contentView. The image moves around when I move the mouse, but as it moves it gets distorted and blurred. Setting up scroll view went fine when I created an NSImageView object and passed it to scrollview as a parameter, but in my case I have to use imageTransform as a document view.
I know this is a novice question and I apologise (because I am novice here :). Setting up scrollview is supposed to be easy but I have tried everything and no luck so far.
I am adding the ImageTransform class code too (this is where image resizing and drawing goes on):
-(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
Upvotes: 1
Views: 680
Reputation: 21
I managed to solve this problem by changing the following line of code:
[self.cIContext drawImage:[self doZooming]
inRect:dirtyRect
fromRect:self.outputImage.extent];
to this:
[self.cIContext drawImage:[self doZooming]
inRect:self.outputImage.extent
fromRect:self.outputImage.extent];
Upvotes: 1