0

有没有办法让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;
  }
}
4

2 回答 2

2

这应该可以解决问题:

-(void)turnoff{

    //YOUT TURN OFF CODE    

}

-(void)doTurnOff{

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

}
于 2012-10-27T21:42:53.957 回答
0

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

于 2012-10-27T23:08:41.913 回答