Re: Forcing NSImageView's centered image not to be drawn on integral pixels?
Re: Forcing NSImageView's centered image not to be drawn on integral pixels?
- Subject: Re: Forcing NSImageView's centered image not to be drawn on integral pixels?
- From: Dan Wood <email@hidden>
- Date: Tue, 10 Dec 2002 10:55:01 -0800
OK, I think I figured it out by myself.
I have a subclass of NSImageView. I override setImage to make sure
that the image is an even number of pixels wide and high.
- (void)setImage:(NSImage *)inImage
{
NSSize theSize = [inImage size];
if (0 != ((int)theSize.width % 2)) { theSize.width ++; }
if (0 != ((int)theSize.height % 2)) { theSize.height++; }
[inImage setScalesWhenResized:NO]; // make the image larger by 1 pixel
without rescaling
[inImage setSize:theSize];
[super setImage: inImage]; // store the main image
}
And I override -[NSView resizeWithOldSuperviewSize:] to force the size
to be an even number of pixels. I have my NSImageView right inside of
another custom view, so I can just size my view to be as big as the
superview.
- (void)resizeWithOldSuperviewSize:(NSSize)oldFrameSize
{
NSRect tSuperRect=[[self superview] frame];
if (0 != ((int)tSuperRect.size.width % 2)) { tSuperRect.size.width
--; }
if (0 != ((int)tSuperRect.size.height % 2)) {
tSuperRect.size.height--; }
[self setFrame:tSuperRect];
}
NSImageView pictures look *so* much better now!
On Tuesday, December 10, 2002, at 10:12 AM, I wrote:
I have an NSImageView with a bitmap NSImage aligned in the center.
This NSImageView grows along with its window, meaning that its size
changes horizontally and vertically, one pixel at a time.
The problem is that the NSImage, to remain perfectly centered,
straddles pixel boundaries much of the time. And this makes the image
look "blurry".
For a sense of what this does, take a look at my test image here:
<http://www.karelia.com/drop/testpat.jpg>
If the image view is an even number of pixels wide and high, then the
centered image looks just like that.
If the image view is an odd number of pixels wide, the image is drawn
at the half pixel, horizontally, and it looks like this:
<http://www.karelia.com/drop/blurry_h.jpg>. You see something similar
in the vertical direction. If both width and height are odd, it looks
even worse: <http://www.karelia.com/drop/blurry_h_and_v.jpg>
So I'm looking for ideas on preventing this from happening. I think
that I need to somehow constrain the NSImageView to only resize in
increments of two pixels so that it will always be an even number of
pixels wide and tall, so that the centered image will be drawn on
integral pixels. Or, somehow intercept the centering behavior of the
NSImageView so that pixels are aligned to their integral boundaries,
even if that means that they aren't perfectly centered.
--
Dan Wood
Karelia Software, LLC
email@hidden
http://www.karelia.com/
Watson for Mac OS X:
http://www.karelia.com/watson/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.