2011-05-04 108 views

回答

-11

你將需要幾個空語句(doSOmethingfoo和showAcitivtyfoo)和的NSTimer(mytimer)在您的.h文件中,有一些代碼

-(void)doSmethingfoo { myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self  selector:@selector(showActivityfoo) userInfo:nil repeats:YES]; } 
+3

這與主題無關。 – 2014-04-08 03:02:38

+1

你喝醉了嗎? – Peter 2017-01-05 00:46:00

77
#import <AVFoundation/AVFoundation.h> 

沿...

- (void) turnTorchOn: (bool) on { 

    // check if flashlight available 
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); 
    if (captureDeviceClass != nil) { 
     AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
     if ([device hasTorch] && [device hasFlash]){ 

      [device lockForConfiguration:nil]; 
      if (on) { 
       [device setTorchMode:AVCaptureTorchModeOn]; 
       [device setFlashMode:AVCaptureFlashModeOn]; 
       //torchIsOn = YES; //define as a variable/property if you need to know status 
      } else { 
       [device setTorchMode:AVCaptureTorchModeOff]; 
       [device setFlashMode:AVCaptureFlashModeOff]; 
       //torchIsOn = NO;    
      } 
      [device unlockForConfiguration]; 
     } 
    } } 
+0

你在哪裏宣佈'torchIsOn'? – Houssni 2013-02-03 17:44:11

+0

@YassineHoussni你可以將它定義爲類名稱中的iVar或屬性,如果你需要getter和setter – Dejell 2013-02-06 12:50:23

+0

@Odelya是的我已經找到了解決方案,我已經將它定義爲一個屬性,謝謝! – Houssni 2013-02-06 12:53:06

23

我結合上述code.it定時器爲我工作...

- (void)viewDidLoad 
     { 
     [super viewDidLoad]; 

     myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self     selector:@selector(toggleFlashlight) userInfo:nil repeats:YES]; 
     // Do any additional setup after loading the view from its nib. 
     } 
     - (void) toggleFlashlight 
     { 

    // check if flashlight available 
    Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); 
    if (captureDeviceClass != nil) { 
     AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
     if ([device hasTorch] && [device hasFlash]){ 

      [device lockForConfiguration:nil]; 
      if (device.torchMode == AVCaptureTorchModeOff) 
      { 
       [device setTorchMode:AVCaptureTorchModeOn]; 
       [device setFlashMode:AVCaptureFlashModeOn]; 
       //torchIsOn = YES; 
      } 
      else 
      { 
       [device setTorchMode:AVCaptureTorchModeOff]; 
       [device setFlashMode:AVCaptureFlashModeOff]; 
       // torchIsOn = NO;    
      } 
      [device unlockForConfiguration]; 
     } 
    } } 
+0

更容易把它放在viewDidAppear中。 – 2015-03-30 16:50:38

相關問題