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: Heinrich Giesen <email@hidden>
- Date: Mon, 30 Oct 2006 17:40:15 +0100
Hello,
On 27.10.2006, at 21:00, Shawn Erickson 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.
For creating images (NSBitmapImageRep objects) with arbitrary resolution
I use a Tiger-only method which is not bound to screen or printer
properties while rendering and does not create a cached rep
(NSCachedImageRep).
I testet the following code to create an NSBitmapImageRep object
with a given size (bSize) and resolution (dpi). I tried it
for rendering Bezier curves in different resolutions.
(For me it worked).
// size is given in dots (= 1/72 inch)
- (void) bezierExampleWithSize:(NSSize)bSize resolution:(float)dpi
{
NSSize imgSize = bSize; // the result image shall have this
size in dots
float scale = dpi/72.0; // nothing to scale for 72 dpi (some
use 96 dpi !)
// a dot has the dimension of a length and is measured in 1/72 inch
// number of pixels corresponding to size and resolution
int pixelsWide = roundf( scale*imgSize.width );
int pixelsHigh = roundf( scale*imgSize.height );
NSBitmapImageRep *newRep =
[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:pixelsWide
pixelsHigh:pixelsHigh
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES // must be even you don't use alpha
isPlanar:NO // planar imageReps are not supported
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0 // let the system use a good value
bitsPerPixel:0 ];
// the new rep still has a resolution of 72 dpi
[NSGraphicsContext saveGraphicsState]; // push the state
// the following is Tiger - only
NSGraphicsContext *context = [NSGraphicsContext
graphicsContextWithBitmapImageRep:newRep];
[NSGraphicsContext setCurrentContext:context];
// set the scaling
NSAffineTransform *transform = [NSAffineTransform
transform]; // identity matrix
[transform scaleBy:scale];
[transform concat];
// ---- start example
// create a Bezier Curve (whatever you want, this is a small
example)
NSBezierPath *bezierPath = [NSBezierPath bezierPath];
// the following values arre given in dots, whatever the
resolution may be
[bezierPath appendBezierPathWithRect:
NSMakeRect( 10, 10, (imgSize.width-20.0),
(imgSize.height-20))];
[[NSColor greenColor] set];
[bezierPath setLineWidth:5.0];
[bezierPath stroke]; // this draws the Bezier curve into newRep
// a bit more example
[[NSColor redColor]set]; // add a small filled red rect
[NSBezierPath fillRect:NSMakeRect( 15, 15, 20.0, 20)];
// ---- end example
// done, restore the graphic state
[NSGraphicsContext restoreGraphicsState]; // pop the state
// finally we set the new size (it's not allowed to do it earlier!)
[newRep setSize:bSize];
// for test purposes only: write it out as a test image
// I use png because it compresses much better than tiff with
LZW compression
// **NO** gif please, gif images know nothing about resolution
// **NO** bmp, bmp images have a resolution field, but it is not
used in Cocoa
// **NO** jpeg, jpeg cannot handle images with high frquencies,
i.e. sharp lines
[[newRep representationUsingType:NSPNGFileType properties:nil]
writeToFile:[NSString stringWithFormat:@"/tmp/bezierImage_%
d.png", (int)dpi]
atomically:YES];
}
But really resolution independent is only an NSEpsImageRep or an
NSPDFImageRep, which
needs some (much) more programming. EPS and PDF render their contents
lazy, i.e. not before
it is needed and after they know about resolution. Will there once be
an NSSVGImageRep ?
Heinrich Giesen
--
Heinrich Giesen
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