Graphics Reference
In-Depth Information
LISTING 7-10
Continued
// Try to open the device
[videoDevice open:&error];
// No need to continue if device couldn't be opened
if
( error !=
nil
)
return
;
// Create a device input object to add to the capture session
QTCaptureDeviceInput *input =
[[QTCaptureDeviceInput
alloc
]
initWithDevice:videoDevice];
[captureSession addInput:input error:&error];
if
( error !=
nil
)
return
;
// Create video output to add raw frames to the session
output = [[QTCaptureDecompressedVideoOutput
alloc
]
init
];
[captureSession addOutput:output error:&error];
if
( error !=
nil
)
return
;
[
self
setSession:captureSession];
}
}
This relatively small amount of code is quite amazing in what it provides. This abstrac-
tions Apple enables you to easily capture and display frames in real time. With
QTCaptureSession
created, the next thing to do is obtain a frame at any given time
by setting a delegate for the
QTCaptureDecompressedVideoOutput
object. After the
QTCaptureDecompressedVideoOutput
object has been allocated, set its delegate to
self
using the following:
[output setDelegate:self];
This enables you to capture the output as an image file or to a QuickTime movie object.
First, however, let's take a closer look at how
QTCaptureLayer
was initialized for the
capture session.
Creating and Displaying the QTCaptureLayer
Although it is possible to create a
QTCaptureLayer
object directly with
alloc
and
init
, or
with a convenience function such as
+layerWithSession
, we're going to subclass
QTCaptureLayer
so that we can hide
QTCaptureSession
's initialization in the
init
. We do