ios - 处理此错误的最佳方法? [AVCaptureFigVideoDevice setTorchModeOnWithLevel :error:]: unrecognized selector sent to instance

标签 ios objective-c flashlight avcapturedevice respondstoselector

如果设备不支持 LED 调光,我正尝试关闭手电筒应用中的图像。

  NSError* outError;
        BOOL success = [device setTorchModeOnWithLevel:brightnessLevel error:&outError];
        if(!success){
            [self.lightDialIndicator setHidden: YES];
            self.lightDial.image = [UIImage imageNamed:@"light_dial_disabled.png"];
        }

但我的应用因以下错误而崩溃

[AVCaptureFigVideoDevice setTorchModeOnWithLevel:error:]: unrecognized selector sent to instance 0x73ad460

当设备不允许我使用 setTorchModeOnWithLevel 时,是否有更好/有效的检测方法?

最佳答案

首先,setTorchModeOnWithLevelAVCaptureDevice 类的一个属性。

其次,如果你想测试一个类是否可以响应你正在调用它的某个选择器,你可以使用这个:

BOOL isSuccessful = NO;
if ([device respondsToSelector:@selector(setTorchModeOnWithLevel:error:)]) {
    NSError* outError;
    isSuccessful = [device setTorchModeOnWithLevel:brightnessLevel error:&outError];
}
if (!isSuccessful) {
    [self.lightDialIndicator setHidden: YES];
     self.lightDial.image = [UIImage imageNamed:@"light_dial_disabled.png"];
}

您没有在您的示例中展示如何实例化 device,但这适用于您不确定它是否具有特定方法的任何类。

关于ios - 处理此错误的最佳方法? [AVCaptureFigVideoDevice setTorchModeOnWithLevel :error:]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16288521/

相关文章:

android - 在 QRcodeScanner React Native 中打开/关闭手电筒 - 替代解决方案

iphone - 复制数组 - 糟糕的设计?

objective-c - 我应该在返回对象之前发送 retain 或 autorelease 吗?

objective-c - 使用转义键忽略具有单个文本字段的工作表

iphone - 如何将 xcode 4.1 iPhone 应用程序转换为通用应用程序

Android:onResume() 跳转到另一个 Activity 而不是异常(exception)

ios - 删除单元格和标题之间的空间

ios - CoreData 关系内容不持久

iphone - 警告 : iPhone apps should include an armv6 architecture (current ARCHS = "armv7")

android - 设置 Parameters.FLASH_MODE_TORCH 在 Droid X 2.3 上不起作用