0

I'm working on getting a camera preview working on 2 different view controllers, which is necessary as a workaround for another SDK I'm using in this app that requires a separate app delegate.

Basically, I've got a camera preview that displays in a UIView that loads on the viewDidLoad method on startup no problem. Then I need to push a button and load a another camera preview that can read and process QR codes. I have separate xibs for the two views, and the child view controller comes up no problem, but the camera preview never loads, so scanning QR codes is pretty hard.

Here's what I've got:

This is the viewDidLoad for the parent ViewController. The camera preview works no problem.

- (void)viewDidLoad {
[super viewDidLoad];
[DRDouble sharedDouble].delegate = self;
NSLog(@"SDK Version: %@", kDoubleBasicSDKVersion);

//capture live preview
session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;

CALayer *viewLayer = cameraPreview.layer;
NSLog(@"viewLayer = %@", viewLayer);

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

captureVideoPreviewLayer.frame = cameraPreview.bounds;
[cameraPreview.layer addSublayer:captureVideoPreviewLayer];

NSArray *possibleDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
AVCaptureDevice *device = [possibleDevices lastObject];

NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
}

[session addInput:input];

[session startRunning];
}

Then I call the child ViewController with this button

- (IBAction)switchScanView {

[session stopRunning];
[session release];

[self presentViewController:[[ViewController alloc] init] animated:true completion:nil];

}

And here's the viewDidLoad from the child View Controller

AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];

if (!input) {
    NSLog(@"%@", [error localizedDescription]);
    return NO;
}

_captureSession = [[AVCaptureSession alloc] init];
[_captureSession addInput:input];

AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[_captureSession addOutput:captureMetadataOutput];

dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create("myQueue", NULL);
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];

viewPreview view's layer.
_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_videoPreviewLayer setFrame:_mviewPreview.layer.bounds];
[_mviewPreview.layer addSublayer:_videoPreviewLayer];


// Start video capture.
[_captureSession startRunning];

I suspect it has something to do with running an AVCaptureSession on two different controllers, but you can see that in the button I have called both [stopRunning] and [release] on the AVCaptureSession object and it's still not coming up.

I'm at a loss of what else to try, does anyone see what is happening?

jamzsabb
  • 1,125
  • 2
  • 18
  • 40

1 Answers1

0

From what you described, and from looking at your code, it seems you are trying to capture from both cameras at the same time, right? You didnt explicitly say that, but I can see in the code that you first do

AVCaptureDevice *device = [possibleDevices lastObject];

Then for the second capture you do this :

AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 

Capturing from both cameras at the same time is not allowed in iOS unfortunately. You just cant do it. Documentation here : https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW14

If that wasnt the intention then my apologies.

Luke Smith
  • 1,218
  • 12
  • 17
  • Appreciate the reply but no that wasn't my intention. The possibleDevices lastObject call is to use the front-facing camera. I'm trying to use the front facing camera in two different UIViews on two different ViewControllers – jamzsabb Aug 20 '15 at 00:19
  • Ahh .. multiple previews. Have come across this issue before, this stackoverflow answer addresses it : http://stackoverflow.com/questions/16543075/avcapturesession-with-multiple-previews – Luke Smith Aug 20 '15 at 06:12
  • I should add - you cant have multiple AVSessions on the same camera like that. But using one of those solutions, you can get your preview view, duplicate it and pass it to the other view controller using delegation. – Luke Smith Aug 20 '15 at 10:20
  • Just to clarify, only one camera preview will be shown at a time. My problem is that I just don't understand the concept of delegation. The whole reason I'm trying to use two different camera previews is because I need to use two different delegates from two different sdk's. After hours of trying to make them work together, I just separated them into two different ViewControllers so i could keep moving. Is all of that really necessary to make two different previews work one at a time? – jamzsabb Aug 20 '15 at 19:22
  • Ive just tried to explain delegation in a comment but I ran out of characters! Delegation is a bit tricky at first but it soon sinks in. I really highly recommend you spend half an hour reading up on it. Its used all the time, everywhere. Heres a quick description : an instance of any class can have a slave that it gets to do stuff for it - receive info, perform tasks, generally be the dogsbody - that poor instance is the delegate. Its used to pass information around, when that information cannot be passed easily - eg the class receiving the info isnt a child of the current property. – Luke Smith Aug 20 '15 at 21:53
  • Haha thanks, I was hoping I'd be able to just get this project done and move on (I'm obviously not the usual Objective C/iOS programmer at work!) but I'm going to have to spend a little more time on this, I'm just making more and more problems for myself with this attitude. I really appreciate all the help though, my code is pretty disgusting at this point, I'm going to do some research and try to get it written correctly. – jamzsabb Aug 25 '15 at 17:44