Skip to Content

devsugar: Accessing an iPhone camera capture session

With the imminent demise of UIGetScreenImage, a number of readers have asked me how they can use the newer AVFoundation approach to access screen data in their iPhone applications. I went ahead and built some sample code that, when I'm finished messing with it, will be part of chapter 7 of my revised cookbook. I have uploaded the current version to github. It consists of a simple helper class that allows you to start and stop a capture session.

You can request an image from this helper (namely, the last captured image from the buffer), which in this example is loaded into a central image view at the end-user's request. You can also ask it for a view with an embedded preview, using the current session. The example project adds that preview to the navigation bar.

I threw this example together pretty quickly, and as always, I welcome suggestions and improvements.

Categories

Developer iPhone

With the imminent demise of UIGetScreenImage, a number of readers have asked me how they can use the newer AVFoundation approach to access...
 

Add a Comment

*0 / 3000 Character Maximum

8 Comments

Filter by:
HansBamb

Hi Erica, thank you for the example. There is just so few information around there.

I've implemented something similar, but I wanted to trigger a captureStillImageAsynchronouslyFromConnection instead of using a low res frame. The problem is that I get a fuzzy image at the end. Continues autofocus is turned on.
Have you got any idea how to get a clear image, just like UIImagePickerController?

So thank you again for your help.

July 27 2010 at 10:17 AM Report abuse rate up rate down Reply
bo

Hi Erica,

Thanks for whipping up the great demo app. I have a couple of questions

1. on 3GS snap function returns an image size of 480.000000 640.000000 and iPhone4 returns an image size of 720.000000 1280.000000. Those seem so arbitrary, are these related to original pixel size of the camera sensor?

2. While running the demo app, I keep on receiving memory warnings, and it crashes when I try to capture very quickly.
2010-07-23 12:54:26.515 HelloWorld[3731:307] Received memory warning. Level=2
Data Formatters temporarily unavailable, will re-try after a 'continue'.
Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib"


Thanks again for the help!

July 23 2010 at 4:03 PM Report abuse rate up rate down Reply
RobbyT

Devkit? :)

July 23 2010 at 8:51 AM Report abuse rate up rate down Reply
Jake Marsh

Great job except for this one bit:

[captureOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

Never ever ever ever EVER use the main dispatch queue. Performance will suffer and Apple just plain tells you not to.

Bad Erica, No Cookie :)

Instead, do something like this:

dispatch_queue_t queue = dispatch_queue_create("com.myapp.tasks.grabcameraframes", NULL);
[captureOutput setSampleBufferDelegate:self queue:queue];

July 22 2010 at 9:33 PM Report abuse rate up rate down Reply
feDe

When I try to compile it the SDK (4.0) throws 22 errors.

July 22 2010 at 6:29 PM Report abuse rate up rate down Reply
1 reply to feDe's comment
Erica Sadun

Set the SDK to 4.0 Device. Update the Info.plist com.yourcompany. Sign with your own credentials.

July 22 2010 at 8:36 PM Report abuse rate up rate down Reply
RobbyT

sorry to side track ..but what happened to your devkit?

July 22 2010 at 12:48 PM Report abuse rate up rate down Reply
Tom Harris

Thanks a lot for this Erica. :)

July 21 2010 at 4:39 PM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.