This is based on the superb example here:
http://red-glasses.com/index.php/tutorials/ios4-take-photos-with-live-video-preview-using-avfoundation/

Adding Live Preview

NOTE – this works for portrait. There seems to currently be an issue making this work with Landscape due to the apple API lacking the required orientation setting

Define in the .h file

@interface MyViewController : UIViewController
{
	IBOutlet UIView *vImagePreview;             //<<<<<ADD THIS
}
@property(nonatomic, retain) IBOutlet UIView *vImagePreview;             //<<<<<ADD THIS
Define in the .m file

@implementation MyViewController

@synthesize vImagePreview;             //<<<<<ADD THIS




//********** VIEW DID UNLOAD **********
- (void)viewDidUnload
{
	[super viewDidUnload];
	
	[vImagePreview release];
	vImagePreview = nil;
}

//********** DEALLOC **********
- (void)dealloc
{
	[vImagePreview release];
	
	[super dealloc];
}
In your Interface Builder .xib file

Add a UIView and link vImagePreview to it.

Create The Live Preview

//*************************************
//*************************************
//********** VIEW DID APPEAR **********
//*************************************
//*************************************
//View about to be added to the window (called each time it appears)
//Occurs after other view's viewWillDisappear
- (void)viewDidAppear:(BOOL)animated
{
	[super viewDidAppear:animated];
	
	
	//----- SHOW LIVE CAMERA PREVIEW -----
	AVCaptureSession *session = [[AVCaptureSession alloc] init];
	session.sessionPreset = AVCaptureSessionPresetMedium;
	
	CALayer *viewLayer = self.vImagePreview.layer;
	NSLog(@"viewLayer = %@", viewLayer);
	
	AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
	
	captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
	[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];
	
	AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
	
	NSError *error = nil;
	AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
	if (!input) {
		// Handle the error appropriately.
		NSLog(@"ERROR: trying to open camera: %@", error);
	}
	[session addInput:input];
	
	[session startRunning];
}

Comments