Reputation: 798
Can anyone please suggest me a good sample code for zooming image in NSImageView. Thanks.
Upvotes: 6
Views: 5005
Reputation: 2172
Unless I misunderstand what you mean by "zooming image" - this seems to be pretty easy. Take the view's frame and scale it at will (although adding limits is obviously highly advisable), while having the image scaling type set to NSImageScaleProportionallyUpOrDown
:
NSRect newFrame = NSMakeRect(newOriginX, newOriginY, newSizeWidth, newSizeHeight);
[myNSImageView setFrame:newFrame];
Upvotes: 0
Reputation: 549
You can embed your image view in an NSScrollView
. Prepare your scroll view like this:
private var scrollView: NSScrollView = {
let view = NSScrollView()
view.allowsMagnification = true
view.minMagnification = 1
view.maxMagnification = 5
return view
}()
and set its documentView to your image view:
scrollView.documentView = myImageView
Upvotes: 2
Reputation: 9169
Perhaps you want to try using ImageKit's IKImageView instead. This gives you zooming, rotation, etc...all for free.
Upvotes: 2
Reputation: 96333
If you look at the documentation, you'll see that NSImageView doesn't support this. Use Image Kit instead.
Upvotes: 3
Reputation: 177
While technically NSImageView doesn't technically support this, it is possible. I believe by changing the bounds of the NSView that you're using, and make sure that the image is set to "Proportionally Down", or "Proportionally Up or Down".
This code seems to work for me, on OSX 10.9 (Mavericks)
NSSize size = [_imageView bounds].size;
NSSize newSize = NSMakeSize(size.width * 0.90, size.height * 0.90);
[_imageView setBoundsSize: newSize];
I have also had success "embedding" an image view inside a scroll view, and then using magnification on the scroll view. YMMV
Upvotes: 2