Capturez des images en arrière-plan?

J'essaie de capturer des images en arrière-plan de la camera sans charger une camera ou une interface de prévisualisation.

Dans mon application, les photos sont sockets en arrière-plan sans écran d'aperçu, juste l'écran d'application normal, puis montré à l'user plus tard.

Quelqu'un peut-il me diriger dans la bonne direction?

Vous devez utiliser AVCaptureSession & AVCaptureDeviceInput.

Cela fait partie du code peut vous aider:

@interface MyViewController : UIViewController { AVCaptureStillImageOutput *_output; AVCaptureConnection *_videoConnection; bool _isCaptureSessionStarted; } @property (retain, nonatomic) AVCaptureDevice *frontalCamera; - (void)takePhoto; 

La mise en oeuvre:

 @interface MyViewController () @end @implementation MyViewController - (id)initWithNibName:(NSSsortingng *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { _isCaptureSessionStarted = false; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Finding frontal camera NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (int i = 0; i < cameras.count; i++) { AVCaptureDevice *camera = [cameras objectAtIndex:i]; if (camera.position == AVCaptureDevicePositionFront) { self.frontalCamera = camera; [self.frontalCamera addObserver:self forKeyPath:@"adjustingExposure" options:NSKeyValueObservingOptionNew context:nil]; [self.frontalCamera addObserver:self forKeyPath:@"adjustingWhiteBalance" options:NSKeyValueObservingOptionNew context:nil]; } } } - (void)observeValueForKeyPath:(NSSsortingng *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (!self.frontalCamera.adjustingExposure && !self.frontalCamera.adjustingWhiteBalance) { if (_isCaptureSessionStarted) { [self captureStillImage]; } } } - (void)takePhoto { if (self.frontalCamera != nil) { // Add camera to session AVCaptureSession *session = [[AVCaptureSession alloc] init]; NSError *error; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:self.frontalCamera error:&error]; if (!error && [session canAddInput:input]) { [session addInput:input]; // Capture still image _output = [[AVCaptureStillImageOutput alloc] init]; // Captured image settings [_output setOutputSettings:[[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil]]; if ([session canAddOutput:_output]) { [session addOutput:_output]; _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; } } if (_videoConnection) { [session startRunning]; NSLock *lock = [[[NSLock alloc] init] autorelease]; [lock lock]; _isCaptureSessionStarted = true; [lock unlock]; } } } else { NSLog(@"%@",[error localizedDescription]); } } } - (void) captureStillImage { [_output captureStillImageAsynchronouslyFromConnection:_videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { NSLock *lock = [[[NSLock alloc] init] autorelease]; [lock lock]; _isCaptureSessionStarted = false; [lock unlock]; if (imageDataSampleBuffer != NULL) { NSData *bitmap = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; // You can get image here via [[UIImage alloc] initWithData:bitmap] } }]; }