
NOTICE: For this tutorial I expect some prior knowledge of Cocoa/Obj-C as well as a knowledge of XCODE and Interface Builder, in particular adding Frameworks to your projects.
Having iSight integrated into you app can be useful in a variety of ways. Whether it be taking photos in apps such as Photo Booth or capturing physical data, such as barcodes in applications like Delicious Library.
The aim of this article is to get you displaying the live output of an iSight ( or rather the systems default video input device ) within your cocoa application. All it takes is a few simple steps!
This article is broken into four sections;
- QTKit - Adding the QTKit framework
- Create an iSight object class. - The code for creating both the .h and .m class files *important bit*
- Interface Builder - Adding the QTSessionView and iSight object class to your Interface Builder file
- Build and Go! - The end of this article
QTKit
QTKit is the name of the framework that we will be using to access the device, display the output and manipulate any of the data. The first step to adding iSight to your application is to add the QTKit.framework to your project.
You can find the framework at “/System/Library/Frameworks/QTKit.framework”
Create an iSight object class.
iSight.h
The second step is to create an iSight object class inside your project. From the XCODE menu choose File - New File - Objective-C Class, name the class iSight and open iSight.h.
First it’s important to import the QTKit header. The final addition to the header file is to create a QTCaptureView IBOutlet (interface builder outlet).
#import <QTKit/QTKit.h>
@interface iSight : NSObject {
IBOutlet QTCaptureView *outputView;
}
@end
Create an iSight object class.
iSight.m
Now that you have imported the QTKit framework and created the output view we can begin working on displaying the actual video output.
For this example project I am going to execute all of my code from within the awakeFromNib method.
@implementation iSight
- (void) awakeFromNib
{
//Our code is going here?!
}
@end
The first object we want to create inside the awakeFromNib method is the QTCaptureSession object. This object manages the video display session.
@implementation iSight
- (void) awakeFromNib
{
//Create the QT capture session
QTCaptureSession *session = [[QTCaptureSession alloc] init];
}
@end
The next step in displaying the video input is creating the QTCaptureDevice object. This object represents the hardware device. Once the object has been created the connection needs to be opened with the open: method.
@implementation iSight
- (void) awakeFromNib
{
//Create the QT capture session
QTCaptureSession *session = [[QTCaptureSession alloc] init];
/* Select the default Video input device */
QTCaptureDevice *iSight = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
/* Passing nil for the NSError parameter may not be the best idea
but i will leave error handling up to you */
[iSight open:nil];
}
@end
This step may seem similar to the previous however, the QTCaptureDeviceInput object we will be using is a bridge between the hardware device object and the QTCaptureSession object.
After creating the object we use the QTCaptureSession’s addInput: method to pass in input data.
@implementation iSight
- (void) awakeFromNib
{
//Create the QT capture session
QTCaptureSession *session = [[QTCaptureSession alloc] init];
/* Select the default Video input device */
QTCaptureDevice *iSight = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
/* Passing nil for the NSError parameter may not be the best idea
but i will leave error handling up to you */
[iSight open:nil];
/* Create a QTKit input for the session using the iSight Device */
QTCaptureDeviceInput *myInput = [QTCaptureDeviceInput deviceInputWithDevice:iSight];
/* Add inputs get the ball rolling etc */
[session addInput:myInput error:nil];
}
@end
The final additions to the iSight.m file we will be adding are two lines of code, the first line setting the QTCaptureView’s session, so it knows to display the visual output. And the second line calls the QTCaptureSession’s startSession method. This gets the ball rolling and displays the video output in the QTCaptureView.
@implementation iSight
- (void) awakeFromNib
{
//Create the QT capture session
QTCaptureSession *session = [[QTCaptureSession alloc] init];
/* Select the default Video input device */
QTCaptureDevice *iSight = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
/* Passing nil for the NSError parameter may not be the best idea
but i will leave error handling up to you */
[iSight open:nil];
/* Create a QTKit input for the session using the iSight Device */
QTCaptureDeviceInput *myInput = [QTCaptureDeviceInput deviceInputWithDevice:iSight];
/* Add inputs get the ball rolling etc */
[session addInput:myInput error:nil];
[outputView setCaptureSession:session];
/* Let the video madness begin */
[session startRunning];
}
@end
Interface Builder.
The final piece of the puzzle is put in place via Interface Builder. The first step is to add the iSight object to you Interface Builder file. The second is to add a QTSessionView to your applications window, this can be found in the standard library of items. Just drag, drop and resize. The final stage in interface builder is to link the iSight objects outputView outlet the the QTSessionView object you have just placed in your window.
Build and Go!
Now that the code had been written and the Interface Builder outlets put in place and linked to the iSight object. It’s time to build and go in XCODE. Sit back and enjoy watching yourself on screen.
Related posts:
- Integrating WebView into a Cocoa app When using a WebView object inside a Cocoa application,...
- Adding a Handle Bar Separator to Your Application This tutorial will teach you how to make the small...
- Relaunching your application Whether you’re applying new preferences, installing a new version...











5 Responses
Great tutorial. Keep up the good work:-)
Thank you heaps - been looking all night for a way to do this (am pretty noob at Xcode
)
Keep up the great work
You totally skipped the Interface Builder section… worthless without it.
There is no need for an interface builder part to this tutorial as you should already understand how to simply drag views into a window if you’re allready looking and image input. I’m sorry if you felt there should be one. But this tutorial covers creating quick time capture sessions and accessing devices through code.
You’re right. I looked it up, it was very easy. Thanks!