Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Get camera preview

I'd like to get the image that is being displayed on the UIImagePickerController when user uses the camera. And when I get I want to process the image and display instead of regular camera view.

But the problem is when I want to get the camera view, the image is just a black rectangle.

Here's my code:

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

UIGraphicsBeginImageContext( CGSizeMake(320, 427) );
[cameraView.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

imageToDisplay.image = [PixelProcessing processImage: viewImage];   //In this case the image is black
//imageToDisplay.image = viewImage; //In this case the image is black too
//imageToDisplay.image = [UIImage imageNamed: @"icon.png"];     //In this case image is being displayed properly

What am I doing wrong?

Thanks.

like image 941
Ilya Suzdalnitski Avatar asked Sep 24 '26 10:09

Ilya Suzdalnitski


2 Answers

This one is also working quite good. Use it when the camera preview is open:

UIImage *viewImage = [[(id)objc_getClass("PLCameraController") 
                      performSelector:@selector(sharedInstance)] 
                      performSelector:@selector(_createPreviewImage)];

But as far as I found out it brings the same results than the following solution which takes a 'screenshot' of the current screen:

extern CGImageRef UIGetScreenImage();

CGImageRef cgoriginal = UIGetScreenImage();
CGImageRef cgimg = CGImageCreateWithImageInRect(cgoriginal, rect);            
UIImage *viewImage = [UIImage imageWithCGImage:cgimg];    
CGImageRelease(cgoriginal);                
CGImageRelease(cgimg);  

A problem I didn't still find a fix for is, how can one get the camera image very fast without any overlays?

like image 181
Holtwick Avatar answered Sep 26 '26 08:09

Holtwick


The unofficial call is:

UIGetScreenImage()

which you declare above the @implementation as:

extern CGImageRef UIGetScreenImage();

There may be a documented way to do this in 3.1, but I'm not sure. If not, please please file a Radar with Apple asking them to make some kind of screen grab access public!!!

That uses your same AppleID you log in to the iPhone development portal with.

Update: This call is not yet documented, but Apple explicitly has given the OK to use it with App Store apps.

like image 21
Kendall Helmstetter Gelner Avatar answered Sep 26 '26 10:09

Kendall Helmstetter Gelner