繁体   English   中英

像本机相机应用程序一样执行闪光(灯光)

[英]Perform a flash (light) like the native camera app does

我如何像本机相机应用一样执行闪光? 我发现以下代码可以打开灯,但是我需要拍摄快照。

if(self.videoDevice.hasTorch) {
    [self.videoDevice lockForConfiguration:nil];
    [self.videoDevice setTorchModeOnWithLevel: 1.0 error: nil];
    [self.videoDevice unlockForConfiguration];
}

您必须将TorchLevel设置在0.0到1.0之间,然后在非常小的间隔内再次设置为0.0。

- (BOOL)setTorchModeOnWithLevel:(float)torchLevel error:(NSError **)outError

试试这个代码:

- (void)turnOnCamerFlash
{
    if (NSClassFromString(@"AVCaptureDevice") != nil)
    {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        if ([device hasTorch])
        {
            [device lockForConfiguration:nil];
            [device setTorchMode:AVCaptureTorchModeOn];
            [device unlockForConfiguration];
        }       
    }
}

它接缝那里没有闪光灯功能,所以执行闪光灯的(唯一的?)可能性是使用计时器。

    - (IBAction)doFlash:(id)sender {
        if(self.videoDevice.hasTorch) {
            flashCounter = 0;
            [NSTimer scheduledTimerWithTimeInterval: 0.1
                                             target:self
                                           selector:@selector(flashLightTicker:)
                                           userInfo:nil
                                            repeats: YES];
        }
    }

    - (void)flashLightTicker:(id)sender {
        [self.videoDevice lockForConfiguration:nil];
        if(flashCounter == 0) {
            [self.videoDevice setTorchModeOnWithLevel: 0.1 error: nil];
        }
        if(flashCounter == 5) {
            [self.videoDevice setTorchMode: AVCaptureTorchModeOff];
        }
        if(flashCounter == 7) {
            [self.videoDevice setTorchModeOnWithLevel: AVCaptureMaxAvailableTorchLevel error: nil];
        }
        if(flashCounter >= 10) {
            [self.videoDevice setTorchMode: AVCaptureTorchModeOff];
            [sender invalidate];
        }
        [self.videoDevice unlockForConfiguration];
        flashCounter++;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM