0

こんにちは、懐中電灯アプリに問題があります。フラッシュをオン/オフするボタンがあります。また、私はviewdidloadでアプリをオンにしました。ホームボタンを押してアプリをもう一度クリックすると、アプリは懐中電灯をオンにしないので、アプリケーションDidBecomeActiveを使用しました。このメソッドはフラッシュをオンにしますが、ボタンをクリックしてオフにすると、アプリがクラッシュします。

これは 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]
4

1 に答える 1

0

これが問題の原因かどうかはわかりませんが、関連している可能性があります。

あなたのtoggleFlashlight方法は、トグルするたびにやりすぎています.1回だけ行う必要があるたびにセットアップを行っています。詳細については、次の例を参照してください。

iPhone でトーチ/フラッシュをオンにする

iWasRobbed による回答を参照してください。そのため、セットアップを一度だけ実行してから、トグルが必要なときに、その例に示すようにsetTorchModeandを呼び出してください。setFlashMode

于 2011-03-13T21:36:06.760 に答える