Re: NSImage and NSImageView questions
Re: NSImage and NSImageView questions
- Subject: Re: NSImage and NSImageView questions
- From: Steve Christensen <email@hidden>
- Date: Sun, 09 May 2010 14:37:38 -0700
On May 8, 2010, at 8:50 AM, Matthew Weinstein wrote:
1. I have an NSImageView which scales the image to fit, aligned
centered. My question is, how do I find the NSRect of the image in
the image view (as it does not fill the frame)?
NSRect scaledImageBounds;
scaledImageBounds.size.width = NSWidth(imageViewBounds);
scaledImageBounds.size.height = NSWidth(imageViewBounds) *
NSHeight(originalImageBounds) / NSWidth(originalImageBounds);
if (NSHeight(scaledImageBounds) > NSHeight(imageViewBounds))
{
scaledImageBounds.size.width *= NSHeight(imageViewBounds) /
NSHeight(scaledImageBounds);
scaledImageBounds.size.height = NSHeight(imageViewBounds);
}
scaledImageBounds.origin.x = NSMidX(imageViewBounds) -
(NSWidth(scaledImageBounds) / 2);
scaledImageBounds.origin.y = NSMidY(imageViewBounds) -
(NSHeight(scaledImageBounds) / 2);
2. Also, I used Apples Cropped Image sample code to create a
selection rectangle, but when I resize the frame the rectangle does
not resize with it. Do I need to do some sort of NSAffineTransform
for that to happen
I haven't looked at the sample code, but I assume there's a custom
NSView that draws the selection area on top of the image? If so, when
the view resizes, I would expect that its -drawRect: method would be
called, giving you an opportunity to update the selection
representation for the new frame size. Is that happening?
If you use a transform, you'll need to do things like adjust the width
of the line used to bound the selection area, FYI. That may or may not
be an issue for you.
3. The selection rectangles need to be saved in a format that is
scale of nsimageview independent, i.e., whenever someone opens this
image they should be able to see the selection view over X portion
of the image immaterial of the size of the image. I am assuming that
I need to use image size to create some absolute transformation of
the selection rectangle and convert back and forth...
Assuming that the selection rectangle always covers some portion of
the original image, why not use a NSRect where the x, y, width, height
have values in the range of 0...1? That way it's easy to convert
between the current displayed image size and the portion of the
original image that's being selected.
steve
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden