0

大家好,我有一个手电筒应用程序的问题。我有一个按钮可以打开/关闭闪光灯。我的 viewdidload 也打开了应用程序。当我按下主页按钮然后再次单击我的应用程序时,应用程序不会打开手电筒,所以我使用了 applicationDidBecomeActive。此方法打开闪光灯,但如果我单击按钮将其关闭,则应用程序崩溃。

这是 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 用于故障排除。按下主页按钮并重新启动应用程序后,即使关闭了闪光灯,控制台也会显示“切换错误”我在这里做错了什么?崩溃日志:

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方法都会做太多事情-每次都在进行设置,而只需进行一次。此处示例中的更多详细信息:

在 iPhone 上打开手电筒/闪光灯

请参阅 iWasRobbed 的答案。所以试着只做一次设置,然后当你需要切换时,调用setTorchModesetFlashMode,如该示例所示。

于 2011-03-13T21:36:06.760 回答