MasterRazer
MasterRazer

Reputation: 1377

Customised flashlight flashing?

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

Upvotes: 0

Views: 474

Answers (2)

hotpaw2
hotpaw2

Reputation: 70733

You can use NSTimer to create a sequence of on and off calls with any duration and interval gap that you need. The NSTimer callback can read the sequence of what to do next and when from an array or plist, etc. It's like building a timed sequential state machine.

Upvotes: 0

Mateus
Mateus

Reputation: 2668

This should do the trick:

-(void)turnoff{

    //YOUT TURN OFF CODE    

}

-(void)doTurnOff{

    [self performSelector:@selector(turnoff) withObject:nil afterDelay:2.0];

}

Upvotes: 2

Related Questions