Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 1.19 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. iPhone flashlight app crashes when button is tapped fast
  2. - (void)toggleFlashlight{
  3.  
  4.   if (isTorchOn) {
  5.  
  6.    // Start session configuration
  7.    [session beginConfiguration];
  8.  
  9.    [device lockForConfiguration:nil];    
  10.    // Set torch to on
  11.    [device setTorchMode:AVCaptureTorchModeOn];
  12.  
  13.    [device unlockForConfiguration];
  14.    [session commitConfiguration];
  15.  
  16.   [session startRunning];
  17.  
  18.  
  19.  }
  20.  else {
  21.   [session stopRunning];
  22.   [session release];
  23.   session = nil;
  24.  
  25.   session = [[AVCaptureSession alloc] init];
  26.  
  27.   // Create device input and add to current session
  28.   AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
  29.   [session addInput:input];
  30.  
  31.   // Create video output and add to current session      
  32.   AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
  33.   [session addOutput:output];
  34.   [output release];
  35.  }
  36. }
  37.        
  38. - (void)torchOnOff: (BOOL) onOff
  39. {
  40.     AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  41.     if ([device hasTorch]) {
  42.         [device lockForConfiguration:nil];
  43.         [device setTorchMode: onOff ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
  44.         [device unlockForConfiguration];
  45.     }
  46. }