converting a NSView into an NSImage, when scaling via scaleUnitSquareToSize ...
converting a NSView into an NSImage, when scaling via scaleUnitSquareToSize ...
- Subject: converting a NSView into an NSImage, when scaling via scaleUnitSquareToSize ...
- From: Martin Redington <email@hidden>
- Date: Fri, 2 Oct 2009 03:38:51 +0100
In order to work around the some of the issues I've been encountering
when scrolling scaled NSTextView's, I've been looking at capturing the
NSTextView as an NSImage, and using that instead (which would meet my
requirements).
I've tried the usual approaches (see below), but none of them are
working as desired, mostly due to the scaling. I'm on 10.5.8, btw.
I've got a test case, where I'm scaling by 4.0 vertically and horizontally.
Without scaling, my view dimensions are: Frame = {{0.00, 0.00},
{900.00, 994.00}}, Bounds = {{0.00, 0.00}, {900.00, 994.00}}
When scaled, my view dimensions are: Frame = {{0.00, 0.00}, {900.00,
16408.00}}, Bounds = {{0.00, 0.00}, {225.00, 4102.00}}
I want to cache an image that I can swap in, in place of the text
view, so I'd expect it to be 900 x 16408, and show the contents of the
scaled text view as they will be drawn on-screen.
For testing, i'm writing the images out using TIFFRepresentation
1) The first NSView to NSImage method uses -[NSView
cacheDisplayInRect:toBitmapImageRep:imageRep]
This produces an image of the correct size (900 x 16408), but the
NSBitMapImageRep reports its size as 225 x 4102
What I actually get on the image looks like its been taken from 0, 0,
255, 4102 in the scaled text view, and scaled again, so that the
partial text fills the entire image, appears four times as large as on
the (scaled) text view.
2) The second method uses lockfocus and -[NSBitmapImageRep
initWithFocusedViewRect:]
This produces a correctly scaled image, but only 666 pixels high (i.e.
the unseen parts of the text view are not drawn).
3) the third method uses lockfocus and drawrect.
This draws the whole NSTextView, but at a resolution of 225 x 4102,
which is all jaggy when scaled up.
Code for each method follows below.
Any suggestions would be great. All I want is to draw the NSView's
contents, with the scaling respected. It doesn't seem like it should
be this hard.
cheers,
m.
======
- (NSImage *)image_method1
{
NSBitmapImageRep *imageRep = [self
bitmapImageRepForCachingDisplayInRect:[self bounds]];
unsigned char *bitmapData = [imageRep bitmapData];
if (bitmapData != NULL)
{
bzero(bitmapData, [imageRep bytesPerRow] * [imageRep pixelsHigh]);
}
[self cacheDisplayInRect:[self bounds] toBitmapImageRep:imageRep];
NSImage *image = [[NSImage alloc] init];
[image addRepresentation: imageRep];
return [image autorelease];
}
- (NSImage *)image_method2
{
[self lockFocus];
NSBitmapImageRep *bits = [[NSBitmapImageRep alloc]
initWithFocusedViewRect: [self bounds]];
[self unlockFocus];
NSImage *image = [[NSImage alloc] init];
[image addRepresentation: bits];
[bits release];
return [image autorelease];
}
- (NSImage *)image_method3
{
// if you use frame here instead of bounds, you get a
correctly sized image, but with the text drawn
// at its original, not scaled size, into the top left corner
of the image.
NSImage *screenshot = [[NSImage alloc] initWithSize:[self bounds].size];
[screenshot setFlipped:YES];
[screenshot lockFocus];
[self drawRect: [self frame]];
[screenshot unlockFocus];
return [screenshot autorelease];
}
--
http://www.mildmanneredindustries.com/
_______________________________________________
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