Graphics Reference
In-Depth Information
LISTING 7-14
Implementing Current Image Capture
- (NSImage*)getCurrentImage;
{
CVImageBufferRef imageBuffer;
@synchronized
(
self
)
{
imageBuffer = CVBufferRetain(currentImageBuffer);
}
if
(imageBuffer) {
// Create an NSImage
NSCIImageRep *imageRep =
[NSCIImageRep imageRepWithCIImage:
[CIImage
imageWithCVImageBuffer:imageBuffer]];
NSImage *image = [[[NSImage
alloc
] initWithSize:
[imageRep
size
]]
autorelease
];
[image addRepresentation:imageRep];
CVBufferRelease(imageBuffer);
return
image;
}
return nil
;
}
Access to the
currentImageBuffer
object is synchronized on a regular basis as it is written
to by the
captureOutput
callback function in Listing 7-13. It runs on its own thread,
making the synchronized block necessary. If the image buffer was successfully retained,
we can convert it to an
NSImage*
and return it to the calling function.
Finally, we add an action to the
AppDelegate
that fires when the Capture Image button is
pressed. The action grabs the current image from the
QTCaptureLayer
-derived class
(
CaptureLayer
) and sets the
NSImageView
's image with it; Listing 7-15 shows how this is
implemented.
LISTING 7-15
Setting the Image View with the Current Image
- (
IBAction
)grabImage:(
id
)sender;
{
NSImage *image = [captureLayer getCurrentImage];
[imageView
setImage
:image];
}