iPhone 应用程序从非活动状态变为活动状态的问题

发布于 2024-10-21 18:16:20 字数 3141 浏览 7 评论 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 用于故障排除。按下主页按钮并重新启动应用程序后,即使闪光灯已关闭,控制台也会显示“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]

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 viewDidUnload];
    // 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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-10-28 18:16:20

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

您的 toggleFlashlight 方法在每次切换时都会执行太多操作 - 它每次都会进行设置,只需完成一次。更多详细信息,请参阅此处的示例:

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

请参阅 iWasRobbed 的回答。因此,请尝试仅进行一次设置,然后当您需要切换时,调用 setTorchModesetFlashMode (如该示例所示)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文