【问题标题】:Customised flashlight flashing?定制手电筒闪烁?
【发布时间】:2012-10-27 21:28:44
【问题描述】:

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

【问题讨论】:

标签: iphone ios flashlight


【解决方案1】:

这应该可以解决问题:

-(void)turnoff{

    //YOUT TURN OFF CODE    

}

-(void)doTurnOff{

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

}

【讨论】:

  • 我会在一秒钟内在我的项目中测试它:D
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多