Re: NSImages rendered at runtime and resolution independent UI...
Re: NSImages rendered at runtime and resolution independent UI...
- Subject: Re: NSImages rendered at runtime and resolution independent UI...
- From: "Ken Ferry" <email@hidden>
- Date: Tue, 31 Oct 2006 02:01:45 -0800
It isn't clear to me if the following drawing code will automatically
create an image 12 by 12 point image with pixel DPI already adjusted
as needed for screen display.
Yep, that's what will happen. Right now, if you lock focus and draw
in an image, you're drawing into a view placed in an offscreen window
(cf. -[NSCachedImageRep window], -[NSCachedImageRep rect]). The
window has the same default scaling between window space and the top
level view as do most windows.
In the future the drawing may not always go into a real window, but
the scaling behavior will be preserved. If you think you need finer
control over your bits, you can draw to an NSBitmapImageRep via
-[NSGraphicsContext graphicsContextWithBitmapImageRep:] or to a
CGBitmapContext.
Ricky adds:
I had many very strange drawing artifacts in those NSImage instances.
There are fixes in this code for Leopard. Please file bugs (like you
said you did), and hopefully we'll get them fixed!
and..
Note that I have done no testing with multi-resolution image files,
so don't know if the Cocoa frameworks will do the right thing (i.e.
which flavor is used depending upon the scale factor).
Again, Cocoa should do the right thing, but there have been a few
fixes. Please file bugs if you see the wrong rep selected, especially
if you happen to be on Leopard.
Ken Ferry
Cocoa frameworks
On 10/27/06, Shawn Erickson <email@hidden> wrote:
I am attempting to get my code ready for resolution independence and I
am trying understand the best way to deal with icon images that I
create at runtime. The below is a cut down example of icon image cache
generation code that I have (the simplest example I have and not one
that would visually benefit the most but...).
Ideally when the user interface scale factor is other then 1.0 I would
want to create an NSImage that is still 12 point by 12 point but has
its pixel content scaled (DPI other then 72) at the time it is created
by the user interface scale factor. I know the framework will scale it
as needed when I go to draw the image but I want to create the image
content at the resolution needed for display to avoid unneeded
interpolation and rep caching.
It isn't clear to me if the following drawing code will automatically
create an image 12 by 12 point image with pixel DPI already adjusted
as needed for screen display (avoiding unneeded interpolation) or
should I modify the code in some fashion to allow the creating of
image content at a higher DPI then 72?
If I need to do the later any recommendations on the best way to go
about doing this?
static NSImage* coloredPenIcons[12] = {nil};
if (coloredPenIcons[0] == nil) {
NSSize imageSize = NSMakeSize(12.0, 12.0); // size in points
NSBezierPath* penBody = [NSBezierPath bezierPath];
[penBody moveToPoint:NSMakePoint(1.0, 3.0)];
...
NSBezierPath* penShadow = [NSBezierPath bezierPath];
[penShadow moveToPoint:NSMakePoint(1.0, 2.0)];
...
NSAffineTransform* transform = [NSAffineTransform transform];
// Translate the origin to the center of the point grid, the
coordinate (0,0)
// is considered the lower left edge of a point in user coordinate space so
// translating by 0.5 will place our drawing centered in the point grid.
[transform translateXBy:0.5 yBy:0.5];
NSColor* shadowColor = [[NSColor colorWithCalibratedRed:COLOR256(150)
green:COLOR256(150)
blue:COLOR256(150)
alpha:1.0] set];
for (int i = 0; i < 12; i++) {
NSImage* image = [[NSImage alloc] initWithSize:imageSize];
[image lockFocus];
[[NSGraphicsContext currentContext] setShouldAntialias:NO];
[transform concat];
[[NSColor colorWithCalibratedRed:COLOR256(150)
green:COLOR256(150)
blue:COLOR256(150)
alpha:1.0] set];
[penShadow stroke];
...color for pen body varies based on index...
[penBody fill];
[penBody stroke];
[image unlockFocus];
coloredPenIcons[i] = image;
}
}
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden