[英]Perform a flash (light) like the native camera app does
How can i perform a flash like the native camera app performs one? 我如何像本机相机应用一样执行闪光? I've found the following code to turn on the light, but i need to take a flash-shot.
我发现以下代码可以打开灯,但是我需要拍摄快照。
if(self.videoDevice.hasTorch) {
[self.videoDevice lockForConfiguration:nil];
[self.videoDevice setTorchModeOnWithLevel: 1.0 error: nil];
[self.videoDevice unlockForConfiguration];
}
Try this one. 试试这个。 Seems like same thing you are expecting
似乎与您期望的一样
http://iosdevelopertips.com/camera/flashlight-application-using-the-iphone-led.html http://iosdevelopertips.com/camera/flashlight-application-using-the-iphone-led.html
您必须将TorchLevel设置在0.0到1.0之间,然后在非常小的间隔内再次设置为0.0。
- (BOOL)setTorchModeOnWithLevel:(float)torchLevel error:(NSError **)outError
Try this code: 试试这个代码:
- (void)turnOnCamerFlash
{
if (NSClassFromString(@"AVCaptureDevice") != nil)
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch])
{
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
}
}
}
It seams that there is no flash-function in there, so the (only?) possibility to perform a flash is to use a timer. 它接缝那里没有闪光灯功能,所以执行闪光灯的(唯一的?)可能性是使用计时器。
- (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.