【问题标题】:iPhone app issue from inactive to active stateiPhone 应用程序从非活动状态到活动状态的问题
【发布时间】:2011-03-13 20:42:46
【问题描述】:

大家好,我的手电筒应用有问题。 我有一个按钮可以打开/关闭闪光灯。我的 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 用于故障排除。按下主页按钮并重新启动应用程序后,即使关闭闪光灯,控制台也会显示“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]

【问题讨论】:

  • 能否请您发布崩溃的堆栈跟踪?

标签: iphone multitasking flashlight


【解决方案1】:

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

您的toggleFlashlight 方法在每次切换时都做的太多——它每次都在做设置,只需要做一次。此处示例中的更多详细信息:

Turn on torch/flash on iPhone

查看 iWasRobbed 的答案。所以试着只做一次设置,然后当你需要切换时,调用setTorchMode和setFlashMode,如该示例所示。

【讨论】:

  • 感谢您的建议,但我不知道如何拆分此代码。有什么建议吗?
  • 刚刚编辑了我的答案以使超链接更加明显,因为它有点不明显。你能在那个链接上看到 iWasRobbed 的答案吗?如果您看到他的策略是什么,那就不言自明了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-08
  • 2015-03-20
  • 2019-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多