Full-screen Camera Viewfinder in iPhone OS 3.1

11.10.09 Posted in iphone by

iPhone OS 3.1 brought several customization options to the UIImagePickerController. With 3.1, you have the ability to customize the behavior of how the camera viewfinder appears to the user, without having to hack around the UIImagePickerController’s cameraView hierarchy. This has given rise to several augmented reality applications, such as the Monocle feature in the Yelp iPhone app.

I’ve been experimenting with a similar augmented reality prototype for a client recently, and was trying to mimic the full-screen viewfinder you see in Yelp’s Monocle. It was easy enough to set up the UIImagePickerController to display the viewfinder and remove the camera controls:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = NO;

However, this gives you a black bar with a height of 56px in the space the camera controls once occupied. To remove the black bar, you actually have to apply a scale transformation to the camera view using the cameraViewTransform property. The scale I’m using is 1.132, which is just enough to cover the 56px gap:

CGAffineTransform cameraTransform = CGAffineTransformMakeScale(1.0, 1.132);
imagePicker.cameraViewTransform = cameraTransform;

It’s a 13% scale, which is only slightly noticeable if you’re comparing the same scene through a non-scaled viewfinder. This transformation will apply to any pictures you might try to take programmatically with -takePicture.

From here, just display / release your UIImagePickerController as you normally would, and you should have a full-screen viewfinder. This only works on iPhone devices, as neither the iPhone Simulator nor the iPod Touch will support the UIImagePickerControllerSourceTypeCamera sourceType.

If you’re looking to build an app that can selectively enable this functionality at run-time, you can test for the sourceType compatibility of the device with +isSourceTypeAvailable:.

2 Responses to “Full-screen Camera Viewfinder in iPhone OS 3.1”

  1. Kai says:

    Hello, brian!

    I just found this post through Google, and it helped my a lot.

    Thanks very much.

    by the way, I am wondering why just use CGAffineTransformMakeScale(1.132, 1.132) instead ?



  2. brian says:

    There’s no reason you couldn’t scale both width and height components if you didn’t want to stretch the height. The key is just to perform the scale transform to hide the gap.

Leave a Reply