tags:

views:

35

answers:

1

Hey guys,

Long story short, every 60th of a second I have a relatively small buffer (256x240) of RGBA data (8 bits per component, 4 bytes per pixel) that is refreshed with new data, and I want to display it (I guess inside an NSView, but anything is welcome). What's the most straightforward way to do it? Building a CGImageRef from a CGBitmapContext to write it to the CGContextRef obtained from the NSGraphicsContext seems a bit convoluted.

+1  A: 

Building a CGImageRef from a CGBitmapContext to write it to the CGContextRef obtained from the NSGraphicsContext seems a bit convoluted.

It is. Specifically, the “bit” is CGBitmapContext; you don't need that if you already have raster data to create an image from. Just create the image, and then draw it into the CGContext you got from the current NSGraphicsContext.

Peter Hosey
Well, the CGBitmapContext bit gets replaced by a CGDataProvider bit. I suppose it's more efficient, but it's not really saving any line of code.
zneak
It's only one line of code to create a data provider from data you already have. For NS/CFData objects: http://developer.apple.com/mac/library/documentation/GraphicsImaging/Reference/CGDataProvider/Reference/reference.html#//apple_ref/doc/c_ref/CGDataProviderCreateWithCFData For bare data buffers: http://developer.apple.com/mac/library/documentation/GraphicsImaging/Reference/CGDataProvider/Reference/reference.html#//apple_ref/doc/c_ref/CGDataProviderCreateWithData
Peter Hosey