iPhone应用程序问题从非活动状态到活动状态

时间:2011-03-13 20:42:46

标签: iphone multitasking flashlight

大家好我手电筒应用有问题。 我有一个按钮来打开/关闭闪光灯。另外我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]

1 个答案:

答案 0 :(得分:0)

不确定这是否会导致您的问题,但可能相关:

每次切换时,toggleFlashlight方法都做得太多了 - 每次都要进行设置,只需要进行一次。此处示例中的更多详细信息:

Turn on torch/flash on iPhone

请参阅iWasRobbed的回答。因此,请尝试仅设置一次设置内容,然后在需要切换时,请调用setTorchModesetFlashMode,如该示例所示。