torchLevel KVO-iOS [英] torchLevel KVO - iOS

查看:103
本文介绍了torchLevel KVO-iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iOS中使用AVFoundation来操纵iPhone 6上的割炬.我需要知道当前的割炬级别.

I am using AVFoundation in iOS to manipulate the torch on an iPhone 6. I need to know the current torch level.

我已阅读

I've read in AppleDevLib that it's possible to observe the current torch level using KVO, but I've failed to implement that.

请问您可以放下一些样本来检测割炬水平的变化,然后再更改变量(例如屏幕上的标签等)吗?这将对我有很大帮助.

Can you put down some sample that detect change of torch level and then changes a variable (like label on screen etc.) please? It will help me a lot.

推荐答案

我花了一些时间弄清楚该怎么做,但我相信此KVO代码将对您有用.我还没有彻底测试过此代码,但我的确可以在应用程序中正常使用它:

It took a while for me to figure out how to do this but I believe this KVO code will work for you. I haven't tested this code out thoroughly but I did get it basically working in an app:

static void * const torchLevelObservationContext = (void*)&torchLevelObservationContext;

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (context == torchLevelObservationContext) {
        AVCaptureDevice *thisDevice = (AVCaptureDevice*)object;
        NSLog( @"Current torch level: %f", thisDevice.torchLevel);
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

-(id) init {
    if (self = [super init]) {
        AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        [videoDevice addObserver:self forKeyPath:@"torchLevel" options:NSKeyValueObservingOptionNew context:torchLevelObservationContext];

        // whatever other initialization code ...
    }
    return self;
}

注意事项:我尝试使用此代码的主要原因是要确定通过控制中心打开的割炬的当前状态,以便在使用AVCaptureDevice和捕获开始后,由于某种原因,割炬将关闭.但是无论我尝试了什么(torchActive,torchMode,torchLevel),我都无法确定状态.该代码只有在控制了AVCaptureDevice并正在使用它的AVCaptureSession之后,似乎才起作用.如果有人知道如何从控制中心获取打开的手电筒或手电筒的当前状态,那就是我想要做的.

Caveat: the main reason I was trying to use this code is to determine the current state of the torch that is turned on via the control center so I could leave it on when using the AVCaptureDevice and the torch is turned off for some reason once the capture starts. But no matter what I tried (torchActive, torchMode, torchLevel), I was unable to determine the state. This code only seems to work after it has taken control of the AVCaptureDevice and is using it an AVCaptureSession. If anyone knows how to get the current state of the flashlight or torch that is turned on from the control center, that is what I am trying to do.

这篇关于torchLevel KVO-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆