2011-03-13 56 views
0

嗨,大家好我有一個手電筒應用程序的問題。 我有一個按鈕來打開/關閉閃光燈。另外我viewdidload打開應用程序。 當我按下主頁按鈕,然後再次點擊我的應用程序時,應用程序不會打開手電筒,因此我使用了applicationDidBecomeActive。這種方法打開閃光燈,但如果我點擊按鈕將其關閉,應用程序崩潰。iPhone應用程序問題從不活動到活躍狀態

這是appdelegate.m:

- (void)applicationDidBecomeActive:(UIApplication *)application{ 
    Just_A_LightViewController *mvc = [[Just_A_LightViewController alloc] init]; 
    [mvc toggleFlashlight]; 
    [mvc release]; 
} 

,這是viewcontroller.m:

- (void)toggleFlashlight{ 
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
    NSLog(@"toggle"); 

if ([device hasTorch] && [device hasFlash]){ 
    NSLog(@"torch and flash"); 

    if (device.torchMode == AVCaptureTorchModeOff) { 
     AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil]; 

     AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init]; 


     AVCaptureSession *session = [[AVCaptureSession alloc] init]; 

     [session beginConfiguration]; 

     [device lockForConfiguration:nil]; 

     [device setTorchMode:AVCaptureTorchModeOn]; 

     [device setFlashMode:AVCaptureFlashModeOn]; 

     [session addInput:flashInput]; 

     [session addOutput:output]; 

     [device unlockForConfiguration]; 

     [output release]; 

     [session commitConfiguration]; 

     [session startRunning]; 

     [self setAVSession:session]; 
     [session release]; 
     NSLog(@"toggle true"); 

    } 
    else 
    { 
     NSLog(@"toggle false"); 
     [AVSession stopRunning]; 
     [AVSession release], AVSession = nil;} 
} 
} 

#pragma mark - View lifecycle 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad 
{ 
    [self toggleFlashlight]; 
    [super viewDidLoad]; 

} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (IBAction)buttonPressed:(id)sender{ 
    NSLog(@"button"); 
[self toggleFlashlight]; 

} 

的nslogs是用於故障排除。在按下主頁按鈕並重新啓動應用程序後,即使關閉了閃光燈,控制檯也會顯示「toggle false」 我在這裏做了什麼錯誤? 系統崩潰日誌:

Sun Mar 13 23:00:05 unknown ReportCrash[313] <Notice>: Formulating crash report for process mediaserverd[21] 
Sun Mar 13 23:00:06 unknown ReportCrash[313] <Error>: Saved crashreport to /var/mobile/Library/Logs/CrashReporter/mediaserverd_2011-03-13-230005_TheMaster.plist using uid: 0 gid: 0, synthetic_euid: 501 egid: 0 
Sun Mar 13 23:00:06 unknown com.apple.launchd[1] <Warning>: (com.apple.mediaserverd) Job appears to have crashed: Bus error 
Sun Mar 13 23:00:06 unknown mediaserverd[314] <Notice>: MS:Notice: Installing: (null) [mediaserverd] (550.52) 
Sun Mar 13 23:00:06 unknown mediaserverd[314] <Notice>: MS:Notice: Loading: /Library/MobileSubstrate/DynamicLibraries/WinterBoard.dylib 
Sun Mar 13 23:00:06 unknown mediaserverd[314] <Warning>: WB:Notice: WinterBoard 
Sun Mar 13 23:00:06 unknown com.apple.mediaserverd[314] <Notice>: MS:Warning: message not found [UIImage defaultDesktopImage] 
+0

您可以請從崩潰發佈堆棧跟蹤? – kennbrodhagen 2011-03-13 20:50:40

回答

0

不知道這是造成您的問題,但也可能是相關的:

toggleFlashlight方法做太多每次切換 - 這是每次都做設置,其中僅需要要做一次。在這裏的例子更多細節:

Turn on torch/flash on iPhone

見iWasRobbed答案。因此,請嘗試僅安裝一次,然後當您需要切換時,請按照該示例中的說明,撥打setTorchModesetFlashMode

+0

感謝您的意見,但我不知道如何分割這段代碼。有什麼建議麼? – user622203 2011-03-13 22:31:28

+0

剛編輯我的答案,使超鏈接更明顯,因爲它有點不明顯。你能看到iWasRobbed在該鏈接上的答案嗎?如果你看到他的策略是什麼,這是不言而喻的。 – occulus 2011-03-13 23:50:05

相關問題