[英]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.