• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Offscreen Drawing Followup
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Offscreen Drawing Followup


  • Subject: Offscreen Drawing Followup
  • From: Carlos Weber <email@hidden>
  • Date: Sun, 10 Jun 2001 16:27:27 -1000

Thanks to everyone who responded to my earlier post requesting info about the Cocoa way of drawing offscreen and getting the bits for future manipulation. Putting together bits and pieces from everyone's suggestions, here are the steps needed to draw offscreen, then retrieve the raw bits (anyone remember Garrison Keilor's ads for "Raw Bits"??) for, e.g., creating a texture in OpenGL:

// 1. create an image...
offImage = [[NSImage alloc]initWithSize: NSMakeSize( whatever )];

// 2. create a window offscreen...
// - bignum can be anything between -16000 and 0
offWindow = [[NSWindow alloc]initWithContentRect:NSMakeRect(- bignum, -bignum, width, height)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreRetained
defer:NO];

// 3. declare a bitmapimagerep
NSBitmapImageRep *offRep;

// 4. lock focus on the window's content view and draw away!!!!
[[offWindow contentView]lockFocus];
[[NSColor greenColor]set];
NSRectFill(NSMakeRect(0,0,512,256));
// or whatever other drawing you want to do...

// 5. allocate and init the bitmapImageRep with the bits you drew into the ether...
offRep = [[NSBitmapImageRep alloc]initWithFocusedViewRect:[[offWindow contentView]bounds]];

// 6. quit drawing into the offscreen view
[[offWindow contentView]unlockFocus];

// 7. now add the imageRep to our image
[offImage addRepresentation: testRep];

At this point you will have an image with one representation (the NSBitmapImageRep you created), and you can use the image to display on the screen (via [NSImageView setImage:], or, as in my case, interrogate the bits via the NSBitmapImageRep's methods ( -bitsPerPixel, -bitmapData, etc.).

A couple of people suggested simply creating an NSImage, locking focus on the image, drawing, and going from there. This works great if you want to display the image on the screen, but the imageRep created by the NSImage in this case is a NSCachedImageRep, which lacks methods for getting at the bitmap data. Hence the need to create an offscreen window (which also creates a default content view), locking focus on the content view and drawing, then explicitly creating and initializing an NSBitmapImageRep with the data from the view.

Thanks again to everyone who contributed to my understanding of this. I hope this longwinded message will save some other Cocoa tyro a few hours of stumbling around thru the AppKit docs.


  • Prev by Date: Re: more nib problems
  • Next by Date: Re: proof of cocoa superiority?
  • Previous by thread: Help with dyld: hidec bad mach header passed to stub_binding_helper
  • Next by thread: Re: proof of cocoa superiority?
  • Index(es):
    • Date
    • Thread