How to show CGImageRef over an overlay nswindow.

  • Hello all,
    I am stuck to a problem related to core graphics. Actually i want to
    display an image over a overlay nswindow. For creating an cgimageref i
    have written the following code:

    CGImageRef  getPNGImage(CFStringRef fileName)
      CGDataProviderRef  provider;
      //CFStringRef    fileName  = NULL;
      CGImageRef      image    = NULL;
      CFURLRef      url    = NULL;
      CFBundleRef    appBundle  = CFBundleGetMainBundle();

      if ( appBundle == NULL )  goto Bail;

      //fileName = CFStringCreateWithCString( NULL, "originalColor.png",
    kCFStringEncodingASCII );  //  ToolbarPoof.png is in our Resources
    directory within the bundle
      if ( fileName == NULL )  goto Bail;

      url = CFBundleCopyResourceURL( appBundle, fileName, NULL, NULL );
      if ( url == NULL ) goto Bail;

      provider  = CGDataProviderCreateWithURL( url );

      image  = CGImageCreateWithPNGDataProvider( provider, NULL, false,
    kCGRenderingIntentDefault );

      CGDataProviderRelease( provider );

      if ( fileName != NULL )  CFRelease( fileName );
      if ( url != NULL )  CFRelease( url );
      return( image );
    But i am not able to display the image on the overlay window.first thing
    is tht i dont know how to put the image on my window still i tried by
    writing following stuff:

    //getting the current context.
    CGContextRef myContext = [[NSGraphicsContext currentContext]
    //nsstring converted to CFStringRef.
    CFStringRef cfFilepath = (CFStringRef)image;
    //calling the above method.
    CGImageRef image = getPNGImage(cfFilepath);
    //drawing the image but where it is displayed i dont know :(.
    CGContextDrawImage(myContext,CGRectMake(0,0,CGImageGetWidth( image
    ),CGImageGetHeight( image )),image);

    In my class where i am implementing this i have the following init method:
    My main problem is how should i display the image over specified location
    ie over the overlay window
    or rather how to put the image over the window so tht it is visible.
    // init method for OverlayWindow class
    -(id)initWithRect:(NSRect)contentRect Controller:(OverlayController
            //dockOrientation = @"";

            if(self = [super initWithContentRect:contentRect
    styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO])
                    //[self setBackgroundColor: [NSColor clearColor]];
                    //[self setAlphaValue:0.00];
                    [self setBackgroundColor:[NSColor redColor]];
                    [self setAlphaValue:0.5];
                    [self setOpaque:NO];
                    [self setHasShadow: NO];
                    [self setLevel:NSStatusWindowLevel];
                    [self makeKeyAndOrderFront:self];
                    [self setIgnoresMouseEvents:NO];

                    // register for required drag types
                    [self registerForDraggedTypes:[NSArray
                    //[trackingWin registerForDraggedTypes:[NSArray
                    //[self setAcceptsMouseMovedEvents:YES];

                    //highlightImage = [NSImage
                    isFocused = NO;
                    //isDragEntered = NO;
                    NSScreen *mainScreen = [NSScreen mainScreen];
                    screenRect = [mainScreen frame];
                    //[self showWindow:YES];
                    overlayController = overlayControl;
                    gOverlayWindow = self;

                    return self;
            return nil;


    Notice: The information contained in this e-mail
    message and/or attachments to it may contain
    confidential or privileged information. If you are
    not the intended recipient, any dissemination, use,
    review, distribution, printing or copying of the
    information contained in this e-mail message
    and/or attachments to it are strictly prohibited. If
    you have received this communication in error,
    please notify us by reply e-mail or telephone and
    immediately and permanently delete the message
    and any attachments. Thank you
  • On 29 Nov 2007, at 12:29, Amit8 J wrote:

    > Hello all,
    > I am stuck to a problem related to core graphics. Actually i want to
    > display an image over a overlay nswindow. For creating an cgimageref i
    > have written the following code:


    You're going about this the hard way.  What you need to do is:

    1. Create your NSWindow.

    2. Add an NSImageView to the new window (e.g. by doing -
    setContentView:).  You'll want to configure it as well (e.g. set the
    frame style, image alignment and image scaling properties).

    3. Use NSImage to load the file (no need to use CGImage; that's just
    complicating matters).  You should be able to do [NSImage
    imageNamed:@"originalColor.png"] to do this.  (Compare with the code I
    chopped out.)

    4. Send your NSImageView a -setImage: message to change its image.

    Also, I don't think you need to subclass the window.  Generally when
    you're writing Cocoa programs, you only subclass when you absolutely
    have to; this is quite a contrast to many other OO frameworks
    (particularly the Windows-based ones) where you tend to end up
    subclassing things all the time.

    Anyway, most of the code you put into your NSWindow subclass could go
    into a method that creates and sets up the window and the image view.

    The other thing that you perhaps didn't understand is that in Cocoa,
    you do your drawing in an NSView subclass's -drawRect:.  Normally,
    anyway.  So if you wanted to do the actual drawing yourself, you'd
    need to write a view that draws an image in its -drawRect:.  Since
    this is a very common requirement, unsurprisingly there is already
    such a view (NSImageView), so there's no need to bother in this
    case... you can just use the ready-built one.

    Kind regards,


previous month november 2007 next month
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today