Re: Creating a bitmap of a scaled-down NSImage
Re: Creating a bitmap of a scaled-down NSImage
- Subject: Re: Creating a bitmap of a scaled-down NSImage
- From: John Stiles <email@hidden>
- Date: Mon, 26 Mar 2007 19:15:02 -0700
Wow. I was really overthinking the problem. Look how easy!
float biggestSize = max( imageSize.width, imageSize.height );
if( biggestSize > 512.0 )
{
float scaleFactor = 512.0 / biggestSize;
imageSize = NSMakeSize( imageSize.width * scaleFactor,
imageSize.height * scaleFactor );
[image setScalesWhenResized:YES];
[image setSize:imageSize];
}
[image lockFocus];
NSBitmapImageRep * bitmap = [[[NSBitmapImageRep alloc]
initWithFocusedViewRect:NSMakeRect(0, 0, imageSize.width,
imageSize.height)] autorelease];
[image unlockFocus];
Works like a charm. You just have to read the docs closely enough to
notice "setScalesWhenResized"—this is NO by default.
I knew the frameworks would have support for something like this.
On Mar 26, 2007, at 6:53 PM, John Stiles wrote:
Actually, I found one way to do it. It's a bit crude, though. I
create an offscreen NSWindow, embed an NSImageView in it, and then
set the NSImageView to show my NSImage. The NSImageView knows how
to scale down. Then I lock on the NSWindow and use
initWithFocusedViewRect on that.
This works, but is there a better way?
Here's a simple code snippet explaining what I mean. It's not
perfect—for non-square images, you can see the gray window
background surrounding the image—but I assume issues like that can
be cleaned up with a little smarts.
NSWindow * s_offscreenWindow = [[[NSWindow alloc]
initWithContentRect:NSMakeRect(0, 0, 512, 512)
styleMask:NSBorderlessWindowMask backing:NSBackingStoreRetained
defer:NO] autorelease];
NSImageView * imageView = [[[NSImageView alloc]
initWithFrame:NSMakeRect(0, 0, 512, 512)] autorelease];
[imageView setImage:image];
[s_offscreenWindow setContentView:imageView];
[[s_offscreenWindow contentView] display];
[[s_offscreenWindow contentView] lockFocus];
NSBitmapImageRep * bitmap = [[[NSBitmapImageRep alloc]
initWithFocusedViewRect:NSMakeRect(0, 0, 512, 512)] autorelease];
[[s_offscreenWindow contentView] unlockFocus];
On Mar 26, 2007, at 6:39 PM, John Stiles wrote:
I have an NSImage that has arrived from an external source that I
don't directly control. I need to scale it down and rasterize it
to an NSBitmapImageRep—the ultimate goal is to get it into an
OpenGL texture, and we have maximum texture-size limits that I
need to respect.
The documentation has some very useful code here: http://
developer.apple.com/documentation/Cocoa/Conceptual/
CocoaDrawingGuide/Images/chapter_7_section_5.html#//apple_ref/doc/
uid/TP40003290-CH208-BCICHFGA
The first technique, using initWithFocusedViewRect, works like a
charm if I don't need to change the image's size. However,
according to the docs, when using this technique, "you cannot
scale the content you capture." And unfortunately, I need to
support 10.3.9, so the NSView techniques listed don't apply to me.
What is the best approach? There must be a way to scale down the
image using the frameworks. I think I could figure out a way to do
it by rasterizing the image and then scaling that bitmap down as a
separate operation, but I'm thinking there must be a better way.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
40blizzard.com
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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