Skip navigation.
 
mlRe: Capturing the Content of a IKImageBrowserView
FROM : Thomas Goossens
DATE : Wed Jan 09 17:24:07 2008

Hello Anthony,

This is because the IKImageBrowserView renders itself into an openGL 
surface.
So you can't retrieve the pixels using the usual AppKit code path.

But instead you can try this:

//1) allocate a c buffer at the size of the  visible rect of the image 
browser
NSRect vRect = [yourImageBrowserView visibleRect];
NSSize size = vRect.size;
   
void *buffer = malloc(size.width * size.height * 4);

//2) read the pixels using openGL
[yourImageBrowserView lockFocus];
glReadPixels(0,
              0,
              size.width,
              size.height,
              GL_RGBA,
              GL_UNSIGNED_BYTE,
              buffer);
[yourImageBrowserView unlockFocus];
   
//3) create a bitmap with those pixels
unsigned char *planes[2];    
planes[0] = (unsigned char *) (buffer);

NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] 
initWithBitmapDataPlanes:planes pixelsWide:size.width 
pixelsHigh:size.height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES 
isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bitmapFormat:0 
bytesPerRow:size.width*4 bitsPerPixel:32];

//4) create a temporary image with this bitmap and set it flipped 
(because openGL and the AppKit don't have the same pixels coordinate 
system)    
NSImage *img = [[NSImage alloc] initWithSize:size];
[img addRepresentation:imageRep];
[img setFlipped:YES];
[imageRep release];
   
//5) draw this temporary image into another image so that we get an 
image without any reference to our "buffer" buffer so that we can 
release it after that
NSImage *finalImage = [[NSImage alloc] initWithSize:size];
[finalImage lockFocus];
[img drawAtPoint:NSZeroPoint 
fromRect:NSMakeRect(0,0,size.width,size.height) 
operation:NSCompositeCopy fraction:1.0];    
[finalImage unlockFocus];
   
//6) release intermediate objects
[img release];
free(buffer);

-- Thomas.


On Jan 9, 2008, at 5:00 PM, Anthony Mittaz wrote:

> Hello,
>
> I'm currently trying to capture the content of a IkImageBrowserView 
> but the image resulting from this operation is always blank.
>
> My code:
>
> [MyIkImageBrowserView lockFocus];
> NSBitmapImageRep *imageRep = [MyIkImageBrowserView 
> bitmapImageRepForCachingDisplayInRect:[MyIkImageBrowserView frame]];
> [MyIkImageBrowserView cacheDisplayInRect:[MyIkImageBrowserView 
> frame] toBitmapImageRep:imageRep];
> [MyIkImageBrowserView unlockFocus];
>
> NSImage *image = [[NSImage alloc] 
> initWithSize:sizeOfMyIkImageBrowserView];
> [image addRepresentation:imageRep];
>
>     
> [imageViewOfTheView setImage:image];
>
> If someone could give me a better solution i would be more than happy.
>
> Thanks
> Anthony Mittaz
> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlCapturing the Content of a IKImageBrowserView Anthony Mittaz Jan 9, 17:00
mlRe: Capturing the Content of a IKImageBrowserView Thomas Goossens Jan 9, 17:24
mlRe: Capturing the Content of a IKImageBrowserView Anthony Mittaz Jan 10, 05:03
mlRe: Capturing the Content of a IKImageBrowserView Alexander Griekspo… Jan 10, 10:58
mlRe: Capturing the Content of a IKImageBrowserView Thomas Goossens Jan 10, 11:36