Graphics Reference
In-Depth Information
this so that we can encapsulate all our capture functionality in the layer and then reuse
the layer wherever we like in our applications that need capture functionality.
In the Photo Capture sample code, we have created this
QTCaptureLayer
derived class and
named it simply
CaptureLayer
. Listing 7-11 shows the
init
code used to initialize the
layer.
LISTING 7-11
QTCaptureLayer Derived Class Initialization
- (
id
)init;
{
self = [super init];
if
( !self )
return nil
;
[
self
initCaptureSession];
return self
;
}
As you can see, we have called
-initCaptureSession
here from Listing 7-10. This way,
when we initialize a new
CaptureLayer
object,
QTCaptureSession
is already fired up and
ready to go. Next, add
CaptureLayer
to the window's root layer tree. You can see how this
code is implemented in the
AppDelegate
's
-awakeFromNib
, as shown in Listing 7-12.
LISTING 7-12
Implementing CaptureLayer in the AppDelegate
-(
void
)awakeFromNib;
{
[[window
contentView
] setWantsLayer:
YES
];
captureLayer = [[CaptureLayer
alloc
]
init
];
// Use the frame from the generic NSView we have
// named captureView
[captureLayer
setBounds
:
NSRectToCGRect([captureView
frame
])];
[captureLayer
setPosition
:
CGPointMake
([captureView
frame
].
size
.
width
/2,
[captureView
frame
].
size
.
height
/2)];
[[captureView
layer
]
insertSublayer
:captureLayer
atIndex
:0];
[captureLayer starCaptureSession];
}