Ho una finestra di anteprima della fotocamera che funziona bene il 90% delle volte. A volte, tuttavia, quando si torna alla mia app, se è in background, l'anteprima non verrà visualizzata. Questo è il codice che io chiamo quando i carichi di vista:AVCaptureSession non riesce quando si torna dallo sfondo
- (void) startCamera {
session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetPhoto;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = _cameraView.bounds;
[_cameraView.layer addSublayer:captureVideoPreviewLayer];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
captureVideoPreviewLayer.position=CGPointMake(CGRectGetMidX(_cameraView.bounds), 160);
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
NSLog(@"ERROR: %@", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Important!"
message:@"Unable to find a camera."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert autorelease];
}
[session addInput:input];
stillImage = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG , AVVideoCodecKey, nil];
[stillImage setOutputSettings:outputSettings];
[session addOutput:stillImage];
[session startRunning];
}
Se questo accade, posso passare a mio modo di vedere le preferenze e viceversa e Al è bene, ma è un fastidioso bug che vorrei uccidere. La finestra di anteprima è un UIView nel mio storyboard.
Grazie, lo farò e lo testò per alcune ore :) – mrEmpty
fantastica soluzione ....... –
Aspetta - non è il caso che 'viewWillAppear' /' Disappear' vengono chiamati solo mentre la scena si muove nella tua app: non vengono chiamate quando l'app entra e esce dal primo piano? Non sarebbe necessario registrarsi per "UIApplicationWillResignActiveNotification" e usarlo? [QA con un esempio di questo] (http://stackoverflow.com/a/22868754/294884) – Fattie