tags:

views:

2778

answers:

7

When I use the standard [view.layer renderInContext:UIGraphicsGetCurrentContext()]; method for turning views into images on the iPhone camera modal view, all I get is the controls with black where the camera preview was.

Anyone have a solution for this? Speed is not an issue.

A: 

This isn't possible unfortunately. For performance the iPhone uses some form of direct rendering to draw the camera preview directly onto the screen instead of as part of a UIView surface. As such when you "capture" the camera view you will only get the UIView elements, not the preview image.

(FWIW this is similar to the reasons why its difficult to screengrab some movie software on Windows/Mac)

Andrew Grant
I think it should be possible, since there are apps in the AppStore that clearly take screenshots of the camera preview window (e.g. QuadCam). If you call UIGetScreenImage() you actually do get an image of the camera preview - but this is a private API function and shouldn't be used.
3n
Yes, UIGetScreenImage grabs the current framebuffer. renderInContext draws the view hierarchy of the specified view into a context. These are conceptually different things.
Andrew Grant
Yes, I understand that. The reason I'm asking is someone by the name "Rolando" has posted that he has a third way to save the camera preview as an image, but claims he can't divulge the information due to an NDA. http://blog.airsource.co.uk/index.php/2008/11/11/views-of-uiimagepickercontroller/
3n
+4  A: 

Not possible from a documented api, but possible. Find the "PLCameraView" view in the camera's subviews, then call

CGImageRef img = (CGImageRef)[foundCameraView imageRef];

This will return a reference to the image that camera holds.

Sophtware
A: 

Can we take the imageRef from camera class itself, I don't want to display camera output to screen and then take the images. I want to process the image and then display it on the screen. is it possible? It will be helpful to see any code snippet. Do you have sample code to do this?

A: 

I tried to use CGImageRef img = (CGImageRef)[cameraView imageRef]; on the PLCameraView object, I got it using this:

UIView *cameraView = [[[[[[self.view subviews] objectAtIndex:0]
     subviews] objectAtIndex: 0]
     subviews] objectAtIndex: 0];

I checked in the debugger that this UIView actually becomes a PLCameraView, so I know I am using the correct object. Still, when sending imageRef to cameraView, I got and interruption of the debugging session ( or crash when running in release mode ).

What I am doing wrong? Im using SDK 2.2.1.

A: 

Sophtware sez:

Not possible from a documented api, but possible. Find the "PLCameraView" view in the camera's subviews, then call

CGImageRef img = (CGImageRef)[foundCameraView imageRef];

This will return a reference to the image that camera holds.

This does not work in OS 3.x ... and based on another reply posted May 25, probably not in OS 2.x either. Sophtware, can you elaborate?

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[PLCameraView imageRef]: unrecognized selector sent to instance 0x138470'

ZeroDiv
A: 

You could try AVCaptureVideoPreviewLayer

pop850
+2  A: 

You can do this with the new AVFoundation stuff in iOS 4.0.

You sued to be able to call UIGetScreenImage() (returns a UIImage) to get a current capture of the whole screen, including the preview. That's how all the barcode apps worked before. But supposedly Apple is disallowing that now and only allow the AVFoundation technique - which only works under 4.0.

The whole reason there's even an issue is because UIGetScreenImage() is not part of the documented API, but Apple made a specific exception for using it. It's not like they are pulling current apps, but they are not allowing new submissions (or updates) that use the older technique.

There is some lobbying on behalf on a number of people to convince Apple to let app developers use the old technique for iOS 3.x only, so send an email to developer relations if you want to use it.

Kendall Helmstetter Gelner