Re: converting a NSView into an NSImage, when scaling via scaleUnitSquareToSize [SOLVED, kinda]
Re: converting a NSView into an NSImage, when scaling via scaleUnitSquareToSize [SOLVED, kinda]
- Subject: Re: converting a NSView into an NSImage, when scaling via scaleUnitSquareToSize [SOLVED, kinda]
- From: Martin Redington <email@hidden>
- Date: Sun, 4 Oct 2009 12:28:24 +0100
Well, I seem to be able to get the image with the following (where
scale is the absolute scale of the view, implemented in the category
defined at http://developer.apple.com/mac/library/qa/qa2004/qa1346.html)
This still feels wrong (note the use of frame instead of bounds for
drawRect call), and I can't help but wonder if drawRect should be
doing the scaling for me automatically ...
- (NSImage *)image_method3
{
NSImage *screenshot = [[NSImage alloc] initWithSize:[self frame].size];
[screenshot setFlipped:YES];
[screenshot lockFocus];
NSSize scale = [self scale];
CGContextScaleCTM([[NSGraphicsContext currentContext] graphicsPort],
scale.width, scale.height);
[self drawRect: [self frame]];
[screenshot unlockFocus];
return [screenshot autorelease];
}
On Fri, Oct 2, 2009 at 3:38 AM, Martin Redington
<email@hidden> wrote:
> 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/
>
--
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