Frederic Mouza
Frederic Mouza

Reputation: 41

iOS : AVFoundation Image Capture Dark

I'm developping an app which captures photos from my iPad front camera. The photos are coming very dark. Does someone have an idea about how to fix this issue, please ?

Here is my code and some explainations :

1) I initialize my capture session

-(void)viewDidAppear:(BOOL)animated{

    captureSession = [[AVCaptureSession alloc] init];
    NSArray *devices = [AVCaptureDevice devices];
    AVCaptureDevice *frontCamera;
    for (AVCaptureDevice *device in devices){
        if ([device position] == AVCaptureDevicePositionFront) {
            frontCamera = device;
        }
    }

    if ([frontCamera isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]){
        NSError *error=nil;
        if ([frontCamera lockForConfiguration:&error]){
            frontCamera.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
            frontCamera.focusMode=AVCaptureFocusModeAutoFocus;
            [frontCamera unlockForConfiguration];
        }
    }

    NSError *error = nil;
    AVCaptureDeviceInput *frontFacingCameraDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:frontCamera error:&error];
    [captureSession addInput:frontFacingCameraDeviceInput];
    [captureSession setSessionPreset:AVCaptureSessionPresetHigh];
    captureVideoOutput = [[AVCaptureVideoDataOutput alloc] init];
    captureImageOutput =[[AVCaptureStillImageOutput alloc] init];
    [captureSession addOutput:captureVideoOutput];
    [captureSession addOutput:captureImageOutput];

}

2) When the user presses the button Record, it starts a timer and preview the content of the camera to a preview layer

- (IBAction)but_record:(UIButton *)sender {

    MainInt = 4;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countup) userInfo:nil repeats:YES];
    previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:captureSession];
    previewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
    CGRect rect = CGRectMake(0, 0, self.aView.bounds.size.width, self.aView.bounds.size.height);
    previewLayer.frame = rect;
    [self.aView.layer addSublayer:previewLayer];
    [captureSession startRunning];

}

3) At the end of the timer, the photo is taken and saved

- (void)countup {
    MainInt -=1;
    if (MainInt == 0)  {      
        [timer invalidate];
        timer = nil;

        [captureSession stopRunning];
        AVCaptureConnection *videoConnection = nil;
        for (AVCaptureConnection *connection in captureImageOutput.connections)
        {
            for (AVCaptureInputPort *port in [connection inputPorts])
            {
                if ([[port mediaType] isEqual:AVMediaTypeVideo] )
                {
                    videoConnection = connection;
                    break;
                }
            }
            if (videoConnection) { break; }
        }

        [captureImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
        {
            CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
            stillImage = [[UIImage alloc] initWithData:imageData];
        }];

        [captureSession startRunning];
        [captureSession stopRunning];
    }  
}

4) Finally, when the user press the save button, the image is recorded in a specific album

- (IBAction)but_save:(UIButton *)sender {
    UIImage *img = stillImage;
    [self.library saveImage:img toAlbum:@"mySpecificAlbum" withCompletionBlock:^(NSError *error)];  
}

In fact, all the code works properly but the resulting images are very dark...

Upvotes: 3

Views: 1852

Answers (2)

Andrew Aarestad
Andrew Aarestad

Reputation: 1160

This was happening to me as well and it turned out I was trying to capture too soon and the camera didn't have enough time to stabilize. I had to add about 0.5 seconds of delay before the pictures would be normal brightness.

HTH

Upvotes: 6

Brenden
Brenden

Reputation: 7908

I had the same issue on an iOS 7 5th gen ipod touch, but not on a 4th gen ipod touch with iOS 6.1.

I found that the fix is to show a preview of the camera:

// Setup camera preview image
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
[_previewImage.layer addSublayer:previewLayer];

As instructed at https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW22

Note: I did not investigate accomplishing this without a preview

Upvotes: 1

Related Questions