How to draw a CGImage object to a NSView?
-
On Feb 20, 2008, at 11:21 PM, Leopard x86 wrote:
> I think it will cost too much performance to convert the CGImage
> objects to other formats, e.g. NSImage, CIImage. So I am wondering a
> direct way to draw CGImage to a NSView or NSOpenGLView.
>
> Can anyone give me a suggestion? Thank you very much in advanced.
Have you considered using an IKImageView? Alternatively, have a look
at CGContextDrawImage().
You might also want to benchmarking going from CGImage -> NSImage.
Depending on the type of images you're working with, and how
frequently you would do this conversion, it might be perfectly usable.
j o a r -
On Feb 20, 2008, at 4:21 PM, Leopard x86 wrote:
> Hi everyone.
> After reading the Quartz Graphics programming document, I am still
> trying to find a method to draw a CGImageRef object "directly" to a
> NSView / NSOpenGLView ...
>
> My application decodes some files on the disk to into
> CGDataProvider, then uses the CGDataProviders to create CGImageRefs.
> I think it will cost too much performance to convert the CGImage
> objects to other formats, e.g. NSImage, CIImage. So I am wondering a
> direct way to draw CGImage to a NSView or NSOpenGLView.
It's pretty easy to do in an NSView:
- (void) drawRect: (NSRect) rect
{
CGRect destRect = CGRectMake(0, 0, 100, 100);
CGContextDrawImage ([[NSGraphicsContext currentContext]
graphicsPort], destRect, myImageRef );
}
Glenn Andreas <gandreas...>
<http://www.gandreas.com/> wicked fun!
quadrium | flame : flame fractals & strange attractors : build,
mutate, evolve, animate -
I would go with:
CGContextDrawImage([[NSGraphicsContext currentContext] graphicsPort,
aRect, theImage);
[NSGraphicsContext currentContext] will return the right graphics
context for drawing into the view.
Mike.
On 20 Feb 2008, at 22:21, Leopard x86 wrote:
> Hi everyone.
> After reading the Quartz Graphics programming document, I am still
> trying to find a method to draw a CGImageRef object "directly" to a
> NSView / NSOpenGLView ...
>
> My application decodes some files on the disk to into
> CGDataProvider, then uses the CGDataProviders to create CGImageRefs.
> I think it will cost too much performance to convert the CGImage
> objects to other formats, e.g. NSImage, CIImage. So I am wondering a
> direct way to draw CGImage to a NSView or NSOpenGLView.
>
> Can anyone give me a suggestion? Thank you very much in advanced.
> Good Luck.
-
On Feb 20, 2008, at 2:21 PM, Leopard x86 wrote:
> Hi everyone.
> After reading the Quartz Graphics programming document, I am still
> trying to find a method to draw a CGImageRef object "directly" to a
> NSView / NSOpenGLView ...
>
> My application decodes some files on the disk to into
> CGDataProvider, then uses the CGDataProviders to create CGImageRefs.
> I think it will cost too much performance to convert the CGImage
> objects to other formats, e.g. NSImage, CIImage. So I am wondering a
> direct way to draw CGImage to a NSView or NSOpenGLView.
CGContextRef context = (CGContextRef)[[NSGraphicsContext
currentContext] graphicsPort];
Then see the Quartz docs for how to draw an image.
--
David Duncan
Apple DTS Animation and Printing
<david.duncan...> -
Hi everyone.
After reading the Quartz Graphics programming document, I am still
trying to find a method to draw a CGImageRef object "directly" to a
NSView / NSOpenGLView ...
My application decodes some files on the disk to into CGDataProvider,
then uses the CGDataProviders to create CGImageRefs. I think it will
cost too much performance to convert the CGImage objects to other
formats, e.g. NSImage, CIImage. So I am wondering a direct way to draw
CGImage to a NSView or NSOpenGLView.
Can anyone give me a suggestion? Thank you very much in advanced.
Good Luck. -
> I think it will cost too much performance to convert the CGImage objects to other formats, e.g. NSImage, CIImage.
It isn't very expensive to make an NSImage from a CGImage. If you
create an NSBitmapImageRep with -initWithCGImage:, the
NSBitmapImageRep will just reference the CGImage and draw it when
necessary. There is no conversion of the data.
-Ken
On Wed, Feb 20, 2008 at 2:21 PM, Leopard x86
<mailinglist.developer...> wrote:
> Hi everyone.
> After reading the Quartz Graphics programming document, I am still
> trying to find a method to draw a CGImageRef object "directly" to a
> NSView / NSOpenGLView ...
>
> My application decodes some files on the disk to into CGDataProvider,
> then uses the CGDataProviders to create CGImageRefs. I think it will
> cost too much performance to convert the CGImage objects to other
> formats, e.g. NSImage, CIImage. So I am wondering a direct way to draw
> CGImage to a NSView or NSOpenGLView.
>
> Can anyone give me a suggestion? Thank you very much in advanced.
> Good Luck.
>
-
..not that you need to do this if you already have a custom view
subclass. Then you should just draw the CGImage from within drawRect,
as glenn described.
-Ken
On Wed, Feb 20, 2008 at 2:29 PM, Ken Ferry <kenferry...> wrote:
>> I think it will cost too much performance to convert the CGImage objects to other formats, e.g. NSImage, CIImage.
>
> It isn't very expensive to make an NSImage from a CGImage. If you
> create an NSBitmapImageRep with -initWithCGImage:, the
> NSBitmapImageRep will just reference the CGImage and draw it when
> necessary. There is no conversion of the data.
>
> -Ken
>
>
>
> On Wed, Feb 20, 2008 at 2:21 PM, Leopard x86
> <mailinglist.developer...> wrote:
>> Hi everyone.
>> After reading the Quartz Graphics programming document, I am still
>> trying to find a method to draw a CGImageRef object "directly" to a
>> NSView / NSOpenGLView ...
>>
>> My application decodes some files on the disk to into CGDataProvider,
>> then uses the CGDataProviders to create CGImageRefs. I think it will
>> cost too much performance to convert the CGImage objects to other
>> formats, e.g. NSImage, CIImage. So I am wondering a direct way to draw
>> CGImage to a NSView or NSOpenGLView.
>>
>> Can anyone give me a suggestion? Thank you very much in advanced.
>> Good Luck.
>>
>
-
On Feb 20, 2008, at 11:34 PM, Ken Ferry wrote:
> ..not that you need to do this if you already have a custom view
> subclass. Then you should just draw the CGImage from within drawRect,
> as glenn described.
...but if the only reason for your subclass is to be able to draw a
CGImage without costly conversions, then I think that you should
consider using [CGImage -> NSBitmapImageRep -> NSImageView], or
IKImageView.
j o a r -
On Feb 20, 2008, at 5:29 PM, Ken Ferry wrote:
>> I think it will cost too much performance to convert the CGImage
>> objects to other formats, e.g. NSImage, CIImage.
>
> It isn't very expensive to make an NSImage from a CGImage. If you
> create an NSBitmapImageRep with -initWithCGImage:, the
> NSBitmapImageRep will just reference the CGImage and draw it when
> necessary. There is no conversion of the data.
It is worth pointing out that an NSBitmapImageRep created in this way
is dependent on the CGImage - the CGImage's lifetime must be at least
that of the dependent bitmap image rep.
http://lists.apple.com/archives/cocoa-dev/2008/Jan/msg01474.html
http://lists.apple.com/archives/cocoa-dev/2008/Jan/msg01481.html
- Jim -
On Wed, Feb 20, 2008 at 3:26 PM, Jim Correia <jim.correia...> wrote:
> On Feb 20, 2008, at 5:29 PM, Ken Ferry wrote:
>
>>> I think it will cost too much performance to convert the CGImage
>>> objects to other formats, e.g. NSImage, CIImage.
>>
>> It isn't very expensive to make an NSImage from a CGImage. If you
>> create an NSBitmapImageRep with -initWithCGImage:, the
>> NSBitmapImageRep will just reference the CGImage and draw it when
>> necessary. There is no conversion of the data.
>
> It is worth pointing out that an NSBitmapImageRep created in this way
> is dependent on the CGImage - the CGImage's lifetime must be at least
> that of the dependent bitmap image rep.
>
> http://lists.apple.com/archives/cocoa-dev/2008/Jan/msg01474.html
>
> http://lists.apple.com/archives/cocoa-dev/2008/Jan/msg01481.html
That's reversed. The limitation is that if you take a CGImage from an
arbitrary NSBitmapImageRep with -CGImage, the CGImage may undetectably
go invalid when the bitmap is destroyed.
Hopefully this issue will be lifted in a future release.
-Ken -
On Feb 20, 2008, at 6:44 PM, Ken Ferry wrote:
> That's reversed. The limitation is that if you take a CGImage from an
> arbitrary NSBitmapImageRep with -CGImage, the CGImage may undetectably
> go invalid when the bitmap is destroyed.
Bah - yes. (In my rush to find the reference, I reversed the situation.)
Specifically, what Troy said was:
"When using -[NSBitmapImageRep CGImage], it's vitally important to
keep the NSBItmapImageRep around for as long as the CGImage is in use.
If you fix that problem, your crashes should disappear."
Jim



