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:05:48 -0700
Could you explain this in a bit more depth?
On Mar 26, 2007, at 6:58 PM, Guy English wrote:
Hi John,
I may be missing something here but is there a reason you can't
just create an NSImage the size you need then draw the captured
views image into it using: [capture drawInRect: smallerImageRect
fromRect: biggerImageRect operation: NSCompositeSourceOver
fraction: 1.0f]; ?
Then get the bitmap image rep from the second, smaller image. It's
basically what you're doing with this offscreen window stuff but
less convoluted.
Guy
On 26-Mar-07, at 9: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:
40gmail.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:
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