2

我正在开发一个从我的 iPad 前置摄像头捕捉照片的应用程序。照片变得很。请问有人知道如何解决这个问题吗?

这是我的代码和一些解释:

1)我初始化我的捕获会话

-(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)当用户按下按钮Record时,它会启动一个定时器并将相机的内容预览到一个预览层

- (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) 定时器结束时,拍照并保存

- (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) 最后,当用户按下保存按钮时,图像被记录在特定相册中

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

事实上,所有代码都可以正常工作,但生成的图像非常暗......

4

2 回答 2

5

这也发生在我身上,结果证明我试图捕捉得太快而相机没有足够的时间稳定下来。我必须添加大约 0.5 秒的延迟才能使图片变为正常亮度。

高温高压

于 2014-04-10T18:02:38.093 回答
1

我在 iOS 7 第 5 代 ipod touch 上遇到了同样的问题,但在 iOS 6.1 的第 4 代 ipod touch 上没有。

我发现解决方法是显示相机的预览:

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

按照https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW22的说明

注意:我没有调查在没有预览的情况下完成此操作

于 2013-09-25T21:28:51.100 回答