gpt4 book ai didi

iphone - 来自多任务时如何使手电筒/闪光灯可靠

转载 作者:行者123 更新时间:2023-11-28 17:42:51 34 4
gpt4 key购买 nike

当我离开应用程序并返回到它的“开启”状态时,有时它可以通过打开手电筒/闪光灯正常工作,但大多数时候它要么闪烁要么保持关闭状态。

AppDeligate.m

- (id) init {
torchState = TRUE;
if( (self=[super init] )) {
/// initialize flashlight
// test if this class even exists to ensure flashlight is turned on ONLY for iOS 4 and above
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

if ([device hasTorch] && [device hasFlash]){

if (device.torchMode == AVCaptureTorchModeOff) {

NSLog(@"Setting up flashlight for later use...");

AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];

AVCaptureSession *session = [[AVCaptureSession alloc] init];

[session beginConfiguration];
[device lockForConfiguration:nil];

[session addInput:flashInput];
[session addOutput:output];

[device unlockForConfiguration];

[output release];

[session commitConfiguration];
[session startRunning];

[self setTorchSession:session];
[session release];
}

}

}
}
return self;
}

- (void)toggleTorch {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
// For the first 4 to 5 times comming back from multiask this first if hits and works properly

if (torchState == TRUE && device.torchMode == AVCaptureTorchModeOff) {
NSLog(@"AVCaptureTorchModeOff setting On");
// On the 4th or 5th time it flashes and stays off or does nothing staying OFF
// even though the NSLog fires

[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];

} else if (torchState == TRUE && device.torchMode == AVCaptureTorchModeOn) {
// Sometimes this randomly fires and every time ErrorNotification fires too
NSLog(@"AVCaptureTorchModeOn");
if (AVCaptureSessionRuntimeErrorNotification) {
NSLog(@"ERROR");
// Try to force but doesn't do anything
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
}
} else {
NSLog(@"ALL IS OFF");
// when torch is in the off state it returns off as it should
torchState = FALSE;
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
}
[device unlockForConfiguration];
}

-(void) applicationDidEnterForeground:(UIApplication*)application {
[self toggleTorch];
}

我唯一没有包含在代码中的是触摸,即使它调用 toggleTorch 来开/关。那件作品再次运行良好,我在这里测试的是在启动时打开它,也就是 DidEnterForeground 以及当应用程序从多任务 session 返回时。

最佳答案

我会改变两件事:1.将其添加到ApplicationDidBecomeActive2. 不要使用切换手电筒,而是设置你想要的状态(这样你就不会把它关掉两次或打开两次......)。所以把函数改成toggleTorchWithState...

这是我在我的一个应用程序中所做的,并且效果很好。

关于iphone - 来自多任务时如何使手电筒/闪光灯可靠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7493173/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com