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...
views:
123answers:
4
+1
A:
I don't believe so, but it's trivial to roll your own NSImageView equivalent that supports center/stretch options by drawing the image yourself.
Andrew Grant
2009-11-07 22:40:57
Could I create an NSView, add a sub NSImageView at a position within?
zekel
2009-11-07 23:28:00
Yeap, or better yet just create an NSView that draws an NSImage at your desired position.
Andrew Grant
2009-11-08 00:13:51
I suppose better yet, crop the NSImage, since I'm slicing an image up into squares.
zekel
2009-11-08 00:37:07
A:
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];
}
zekel
2009-11-08 01:21:08
+1
A:
Make your imageview as big as the image, and put it inside a scrollview. Hide the scrollers if you want. No need for subclassing in this case.
NSResponder
2009-11-08 04:54:57
+1
A:
NSImageView has a method -setImageAlignment:
which lets you control how the image is aligned within the image view. Unfortunately, if you want to display part of the image that doesn't correspond to any of the NSImageAlignment
values, you're going to have to draw the image programmatically.
Kevin Ballard
2009-11-08 08:18:00
Right. I want to slice up a big image into little boxes, so basic image alignment won't do it in my case.
zekel
2009-11-09 15:01:15