嗨,大家好我有一個手電筒應用程序的問題。 我有一個按鈕來打開/關閉閃光燈。另外我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]
您可以請從崩潰發佈堆棧跟蹤? – kennbrodhagen 2011-03-13 20:50:40