0

我怎样才能像原生相机应用程序一样执行闪光灯?我找到了以下代码来打开灯,但我需要拍一张闪光灯。

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

4 回答 4

0

试试这个。看起来和你期待的一样

http://iosdevelopertips.com/camera/flashlight-application-using-the-iphone-led.html

于 2013-04-23T09:44:51.397 回答
0

您必须将 torchLevel 设置在 0.0 到 1.0 之间,然后再设置为 0.0,时间间隔非常小。

- (BOOL)setTorchModeOnWithLevel:(float)torchLevel error:(NSError **)outError
于 2013-04-23T09:44:53.667 回答
0

试试这个代码:

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

        if ([device hasTorch])
        {
            [device lockForConfiguration:nil];
            [device setTorchMode:AVCaptureTorchModeOn];
            [device unlockForConfiguration];
        }       
    }
}
于 2013-04-23T09:51:51.603 回答
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++;
    }
于 2013-04-23T10:52:29.157 回答