快速点击按钮时 iPhone 手电筒应用程序崩溃

发布于 2024-10-13 00:34:39 字数 981 浏览 3 评论 0原文

我的手电筒应用程序完美运行。但出于某种原因,每次我非常快地点击手电筒按钮时,该应用程序就会冻结并且不执行任何操作。当我调用 AVCaptureSession stopRunning 时,它似乎冻结了。这是我的切换手电筒方法的代码。我还希望最终为选通功能调用此方法。

- (void)toggleFlashlight{

  if (isTorchOn) {

   // Start session configuration
   [session beginConfiguration];

   [device lockForConfiguration:nil];    
   // Set torch to on
   [device setTorchMode:AVCaptureTorchModeOn];

   [device unlockForConfiguration];
   [session commitConfiguration];

  [session startRunning];


 }
 else {
  [session stopRunning];
  [session release]; 
  session = nil;

  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];
  [output release];
 }
}

I got my flashligh app working perfectly. For some reason tho, everytime i tap on the flashlight button really fast, the app just freezes and doesnt do anything. It seems that its freezing when i call the AVCaptureSession stopRunning. Heres the code for my toggle flashlight method. I also want this method to eventually be called for a strobe feature.

- (void)toggleFlashlight{

  if (isTorchOn) {

   // Start session configuration
   [session beginConfiguration];

   [device lockForConfiguration:nil];    
   // Set torch to on
   [device setTorchMode:AVCaptureTorchModeOn];

   [device unlockForConfiguration];
   [session commitConfiguration];

  [session startRunning];


 }
 else {
  [session stopRunning];
  [session release]; 
  session = nil;

  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];
  [output release];
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

西瑶 2024-10-20 00:34:39

您真的了解上面粘贴的代码的作用吗,还是从其他地方复制的?

因为您所做的就是反复要求 iOS 创建视频捕获会话,然后将灯打开。创建视频捕获会话需要大量 CPU/内存。它从来没有被设计或打算快速使用 - 不存在用户尝试在几秒钟内启动多个视频捕获会话的“正常”用例。这就是你崩溃的原因。

Do you actually understand what the code you've pasted above does, or did you copy it from somewhere else?

Because what you're doing is repeatedly asking iOS to create a video capture session, which then turns the light on. Creating a video capture session is very CPU/memory intensive. It was never designed or intended to be used rapidly - there's no 'normal' use-case in which a user would attempt to initiate multiple video capture sessions within seconds of each other. That's why you're getting your crashes.

你如我软肋 2024-10-20 00:34:39

这是一个较短的版本,您现在可以使用它来打开或关闭 LED:

- (void)torchOnOff: (BOOL) onOff
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]) {
        [device lockForConfiguration:nil];
        [device setTorchMode: onOff ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
        [device unlockForConfiguration];
    }
}

Here is a shorter version you can now use to turn the LED on or off:

- (void)torchOnOff: (BOOL) onOff
{
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([device hasTorch]) {
        [device lockForConfiguration:nil];
        [device setTorchMode: onOff ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
        [device unlockForConfiguration];
    }
}
不气馁 2024-10-20 00:34:39

如果您的应用程序围绕 AVCaptureSession 展开,则您应该在应用程序的生命周期内运行一个 AVCapture 会话实例,并且只需在该会话中切换火炬即可。你把事情搞得太复杂了。
如前所述,不要继续重新创建同一个会话。这是资源的浪费。我建议观看有关该主题的 WWDC '10 视频并浏览演示项目。

If your app revolves around AVCaptureSession, you should have one instance of AVCapture session running for the life of the app and simply toggle the torch in that session. You're over complicating things.
As mentioned, do NOT keep recreating the same session. This is a waste of resources. I recommend watching the WWDC '10 videos on the subject and looking through the Demo projects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文