开发者

iPhone app issue from inactive to active state

开发者 https://www.devze.com 2023-02-16 09:05 出处:网络
Hi guys I have an issue with a flashlight app. I have a button to turn on/off the flash. Also I the viewdidload turn on the app.

Hi guys I have an issue with a flashlight app. I have a button to turn on/off the flash. Also I the viewdidload turn on the app. When I press the home button and then click on my app again the app dont turn on the flashlight so I have used the applicationDidBecomeActive. This method turn on the flash but if I click on the button to turn it off the app is crashing.

this is the appdelegate.m:

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

and this is the 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 viewDidUn开发者_如何转开发load];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

}

The nslogs is for troubleshooting. After pressing the home button and relaunching the app even if the flash is turned off the console shows the "toggle false" What I am doing wrong here? Crashlog:

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]


Not sure if this is causing your issue, but it might be relevant:

Your toggleFlashlight method is doing too much each time you toggle -- it's doing setup each time which only needs to be done once. More details in the example here:

Turn on torch/flash on iPhone

See the answer by iWasRobbed. So try doing the setup stuff only once, and then when you need to toggle, call setTorchMode and setFlashMode as shown in that example.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号