Is there a way to let the flashlight of the iPhone flashing several times after clicking a button? Like if I click a button the flashlight flashes only 3 times? I havent found information about this on the web. Could anyone help me with this? And is there a way to make the flashes longer? like 2 seconds flash?
I dont know if someone of you dont understand what I mean: I want to turn on the flashlight for just 2 seconds and after 2 seconds it turns off itself.
My code is at the time:
- (void)loadView
{
[self setView:[[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// If torch supported, add button to toggle flashlight on/off
if ([device hasTorch] == YES)
{
flashlightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 120, 320, 98)];
[flashlightButton setBackgroundImage:[UIImage imageNamed:@"TorchOn.png"] forState:UIControlStateNormal];
[flashlightButton addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[[self view] addSubview:flashlightButton];
}
}
and to turn on and off:
- (void)buttonPressed:(UIButton *)button
{
if (button == flashlightButton)
{
if (flashlightOn == NO)
{
flashlightOn = YES;
[flashlightButton setBackgroundImage:[UIImage imageNamed:@"TorchOff.png"] forState:UIControlStateNormal];
}
else
{
flashlightOn = NO;
[flashlightButton setBackgroundImage:[UIImage imageNamed:@"TorchOn.png"] forState:UIControlStateNormal];
}
[self toggleFlashlight];
}
}
and for simulating taking picture:
- (void)toggleFlashlight
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device.torchMode == AVCaptureTorchModeOff)
{
// Create an AV session
AVCaptureSession *session = [[AVCaptureSession alloc] init];
// Create device input and add to current session
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
[session addInput:input];
// Create video output and add to current session
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];
// Start session configuration
[session beginConfiguration];
[device lockForConfiguration:nil];
// Set torch to on
[device setTorchMode:AVCaptureTorchModeOn];
[device unlockForConfiguration];
[session commitConfiguration];
// Start the session
[session startRunning];
// Keep the session around
[self setAVSession:session];
[output release];
}
else
{
[AVSession stopRunning];
[AVSession release], AVSession = nil;
}
}