0

I'm attempting to build a custom camera app.

Tasks:

  1. Shows an live image preview.
  2. Allows you to tap anywhere on the screen to capture an image.
  3. Displays the resulting image above the live image preview.

Currently everything works except for task 1. The view that contains my live image preview remains it's background color (or remains transparent when no background color is selected), even though the camera is operational and an image is displayed when the screen is tapped. Any ideas? I've referred to this previous discussion and I think I'm covering all my bases: AVFoundation camera preview layer not working

What am I missing?

-(void)viewWillAppear:(BOOL)animated{
    session = [[AVCaptureSession alloc] init];
    [session setSessionPreset:AVCaptureSessionPresetPhoto];

    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error;
    AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];

    if ([session canAddInput:deviceInput]) {
        [session addInput:deviceInput];
    }
    AVCaptureVideoPreviewLayer *previewlayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    [previewlayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    CALayer *rootLayer = [[self view] layer];
    [rootLayer setMasksToBounds:YES];
    CGRect frame = self.frameforCapture.frame;

    [previewlayer setFrame:frame];

    [rootLayer insertSublayer:previewlayer atIndex:0];

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];

    [session addOutput:stillImageOutput];

    [session startRunning];
    //....
}
Community
  • 1
  • 1
  • Have you checked the frame to see what it actually is? You may want to try `[previewlayer setFrame:self.view.bounds]; ` – Dima Aug 18 '14 at 17:43
  • Thanks for your response. I tried your recommendation, not much luck. Currently, my frame = frameforCapture, which I have defined in my .h file as connected to a view object: @property (strong, nonatomic) IBOutlet UIView *frameforCapture; – chickenorbeef Aug 18 '14 at 18:07

0 Answers0