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

时间:2013-04-23 09:38:27

标签: ios objective-c flash

我如何执行像本机相机应用程序执行一个闪存? 我找到了以下代码来打开灯,但我需要拍摄闪光灯。

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

4 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

你必须将torchLevel设置在0.0到1.0之间,然后再设置为0.0,间隔很小。

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

答案 2 :(得分:0)

试试这段代码:

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

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

答案 3 :(得分:0)

似乎没有闪光功能,所以执行闪光灯的唯一可能性就是使用计时器。

    - (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++;
    }