Skip navigation.
 
mlRe: Converting CGImageRefs to NSImages, how?
FROM : Andrew Platzer
DATE : Mon Apr 25 22:59:28 2005

On Apr 24, 2005, at 5:05 PM, Ken Tozier wrote:
> Sorry if this is an easy one but a thorough search of the list 
> archives and the web didn't yield anything of much help.
>
> I'm trying to convert a CGImageRef returned from "[context 
> createCGImage: img fromRect: imgRect]"
> to an NSImage so it can be used as the image in a drag operation 
> but can't find any way to do the conversion.


Simple. Draw it using CG in a lock focus view. The NSGraphicsContext 
has a 'graphicsPort' that gives you the CGContextRef. (The CGRect 
coercion is to make the compiler happy even though CGRect has the 
same struct layout as NSRect)

    NSImage* image = [[NSImage alloc] initWithSize:rect.size];
    [image lockFocus];
    CGContextDrawImage([[NSGraphicsContext currentContext] 
graphicsPort], *(CGRect*)&rect, imageRef);
    [image unlockFocus];

Andrew Platzer
Application Frameworks
Apple Computer, Inc.

Related mailsAuthorDate
mlConverting CGImageRefs to NSImages, how? Ken Tozier Apr 25, 02:05
ml[Solved]: Converting CGImageRefs to NSImages, how? Ken Tozier Apr 25, 07:35
mlRe: Converting CGImageRefs to NSImages, how? Sean McBride Apr 25, 20:25
mlRe: Converting CGImageRefs to NSImages, how? Andrew Platzer Apr 25, 22:59
mlRe: Converting CGImageRefs to NSImages, how? Ken Tozier Apr 26, 03:59
mlRe: Converting CGImageRefs to NSImages, how? Sean McBride Apr 26, 15:57
mlRe: Converting CGImageRefs to NSImages, how? Andrew Platzer Apr 27, 20:53
mlRe: Converting CGImageRefs to NSImages, how? Scott Thompson Apr 27, 21:16