定制手电筒闪烁?

时间:2012-10-27 21:28:45

标签: iphone ios flashlight

点击按钮后,有没有办法让iPhone的手电筒闪烁几次? 就像我点击按钮一样,手电筒只闪3次? 我没有在网上找到有关此信息。 任何人都可以帮我这个吗? 有没有办法让闪光更长?喜欢2秒闪光?

我不知道你是否有人不理解我的意思:我想打开手电筒只需2秒钟,2秒后它会自动关闭。

我的代码当时是:

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

并打开和关闭:

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

用于模拟拍照:

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

2 个答案:

答案 0 :(得分:2)

这应该可以解决问题:

-(void)turnoff{

    //YOUT TURN OFF CODE    

}

-(void)doTurnOff{

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

}

答案 1 :(得分:0)

您可以使用NSTimer创建一系列打开和关闭呼叫,其中包含您需要的任何持续时间和间隔。 NSTimer回调可以读取下一步以及何时从数组或plist等执行的序列。这就像构建定时顺序状态机一样。