前置闪光灯亮度iPhone

时间:2015-03-07 04:02:48

标签: ios iphone image avcapturesession avcapturedevice

我已经在一个项目上工作了一段时间,我已经找到了一件我想要真正解决的事情,但是我还没有能够找到答案。

在拍摄正面照片的应用程序中,我希望前置闪光灯能够让照片更亮。

我使用的是自定义AVCaptureSession相机,它是全屏的。以下代码确实可以实现闪光,只是图片根本不亮。

//Here is the code for a front flash on the picture button press. It does flash, just doesn't help.
UIWindow* wnd = [UIApplication sharedApplication].keyWindow;
UIView *v = [[UIView alloc] initWithFrame: CGRectMake(0, 0, wnd.frame.size.width, wnd.frame.size.height)];
[wnd addSubview: v];
v.backgroundColor = [UIColor whiteColor];
[UIView beginAnimations: nil context: nil];
[UIView setAnimationDuration: 1.0];
v.alpha = 0.0f;
[UIView commitAnimations];

//imageView is just the actual view the the cameras image fills.
imageView.hidden = NO;
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections) {
    for(AVCaptureInputPort *port in [connection inputPorts]) {
        if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
            videoConnection = connection;
            break;
        }
    }if (videoConnection) {
        break;
    }

}   [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
    if (imageDataSampleBuffer != NULL) {
        imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        UIImage *thePicture  = [UIImage imageWithData:imageData];
        self.imageView.image = thePicture;
 //After the picture is on the screen, I just make sure some buttons are supposed to be where they are supposed to be.
        saveButtonOutlet.hidden = NO;
        saveButtonOutlet.enabled = YES;
        diaryEntryOutlet.hidden = YES;
        diaryEntryOutlet.enabled = NO;
    }

}];

}

1 个答案:

答案 0 :(得分:1)

您需要在捕获图像之前将屏幕设置为白色,等待捕获完成,然后删除完成块中的白色屏幕。

您还应该在短暂延迟后发送捕获,以确保屏幕变白 -

UIWindow* wnd = [UIApplication sharedApplication].keyWindow;
UIView *v = [[UIView alloc] initWithFrame: CGRectMake(0, 0, wnd.frame.size.width, wnd.frame.size.height)];
[wnd addSubview: v];
v.backgroundColor = [UIColor whiteColor];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
        if (imageDataSampleBuffer != NULL) {
            imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *thePicture  = [UIImage imageWithData:imageData];

            dispatch_async(dispatch_get_main_queue(), ^{
                self.imageView.image = thePicture;
                [v removeFromSuperview];
            });
        }
        //After the picture is on the screen, I just make sure some buttons are supposed to be where they are supposed to be.
        saveButtonOutlet.hidden = NO;
        saveButtonOutlet.enabled = YES;
        diaryEntryOutlet.hidden = YES;
        diaryEntryOutlet.enabled = NO;
    }];
}];