Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take a picture on iPhone without showing controls

Tags:

Is there a way to take a picture in code on the iPhone without going through the Apple controls? I have seen a bunch of apps that do this, but I'm not sure what API call to use.

like image 374
Rob Bonner Avatar asked Mar 08 '11 20:03

Rob Bonner


People also ask

How do I hide the camera control on my iPhone?

Once you have the controls showing, tap a button to access its options, use a slider or button to adjust the settings ➌, tap the original button to hide the options, and swipe down in the same way you swiped up to hide all the controls.

Can iPhone take a picture without having to press a button?

All that you have to do is open the iPhone's Camera, switch for the front-facing selfie lens, frame the image and press Volume Up or Volume Down to capture the picture! Tip: You can quickly open the iPhone's Camera using this Lock Screen shortcut!

How do you take a picture without pushing the button?

Behold, Snapi. It's an Android-based app developed by eyeSight that features gesture recognition, so you will never drop your phone while trying to click a button again. All you have to do is access the app on your smartphone and pose for your photo while holding an open palm in front of the camera.

How do I manually take photos on my iPhone?

Take a photo or videoTap the Shutter button, or press either volume button to take a photo. Tip: If you want to take a video while you're in Photo mode, touch and hold the Shutter button to record a QuickTake video (iPhone 11 and later).


2 Answers

EDIT: As suggested in the comments below, I have now explicitly shown how the AVCaptureSession needs to be declared and initialized. It seems that a few were doing the initialization wrong or declaring AVCaptureSession as a local variable in a method. This would not work.

Following code allows to take a picture using AVCaptureSession without user input:

// Get all cameras in the application and find the frontal camera. AVCaptureDevice *frontalCamera; NSArray *allCameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];  // Find the frontal camera. for ( int i = 0; i < allCameras.count; i++ ) {     AVCaptureDevice *camera = [allCameras objectAtIndex:i];      if ( camera.position == AVCaptureDevicePositionFront ) {         frontalCamera = camera;     } }  // If we did not find the camera then do not take picture. if ( frontalCamera != nil ) {     // Start the process of getting a picture.     session = [[AVCaptureSession alloc] init];      // Setup instance of input with frontal camera and add to session.     NSError *error;     AVCaptureDeviceInput *input =     [AVCaptureDeviceInput deviceInputWithDevice:frontalCamera error:&error];      if ( !error && [session canAddInput:input] ) {         // Add frontal camera to this session.         [session addInput:input];          // We need to capture still image.         AVCaptureStillImageOutput *output = [[AVCaptureStillImageOutput alloc] init];          // Captured image. settings.         [output setOutputSettings:         [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]];          if ( [session canAddOutput:output] ) {             [session addOutput:output];              AVCaptureConnection *videoConnection = nil;             for (AVCaptureConnection *connection in output.connections) {                 for (AVCaptureInputPort *port in [connection inputPorts]) {                     if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {                         videoConnection = connection;                         break;                     }                 }                 if (videoConnection) { break; }             }              // Finally take the picture             if ( videoConnection ) {                 [session startRunning];                  [output captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {                      if (imageDataSampleBuffer != NULL) {                         NSData *imageData = [AVCaptureStillImageOutput                                    jpegStillImageNSDataRepresentation:imageDataSampleBuffer];                         UIImage *photo = [[UIImage alloc] initWithData:imageData];                                                                                      }                  }];             }         }     } } 

session variable is of type AVCaptureSession and has been declared in .h file of the class (either as a property or as a private member of the class):

AVCaptureSession *session; 

It will then need to be initialized somewhere for instance in the class' init method:

session = [[AVCaptureSession alloc] init] 
like image 191
Khurram Ali Avatar answered Sep 21 '22 18:09

Khurram Ali


Yes, there are two ways to do this. One, available in iOS 3.0+ is to use the UIImagePickerController class, setting the showsCameraControls property to NO, and setting the cameraOverlayView property to your own custom controls. Two, available in iOS 4.0+ is to configure an AVCaptureSession, providing it with an AVCaptureDeviceInput using the appropriate camera device, and AVCaptureStillImageOutput. The first approach is much simpler, and works on more iOS version, but the second approach gives you much greater control over photo resolution and file options.

like image 36
Drew C Avatar answered Sep 19 '22 18:09

Drew C