调用设备闪光灯

调用设备闪光灯,使用“AVFoundation.framework”框架里的“AVCaptureDevice”来调用设备闪光灯。

详细操作步骤如下:

步骤1导入“AVFoundation.framework”框架

步骤2引入头文件“<AVFoundation/AVFoundation.h>”, #import <AVFoundation/AVFoundation.h>

步骤3初始化AVCaptureDevice实例

AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

步骤4异常判断,判断设备是否支持闪光灯功能

4-1 [captureDevice hasTorch]

4-2 [captureDevice hasFlash]

步骤5闪光灯的打开或关闭

[captureDevice lockForConfiguration:nil];

[captureDevice setTorchMode: AVCaptureTorchModeOff]; //打开或关闭

[captureDevice unlockForConfiguration];


详细代码:

.h文件

@interface FreshlightManager : NSObject

- (void)showFreshlight;

@end

.m文件

#import <AVFoundation/AVFoundation.h>

@implementation FreshlightManager

- (void)showFreshlight
{
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    if ([captureDevice hasTorch] && [captureDevice hasFlash])
    {
        if (captureDevice.torchMode == AVCaptureTorchModeOff)
        {
            [captureDevice lockForConfiguration:nil];
            [captureDevice setTorchMode: AVCaptureTorchModeOn];
            [captureDevice unlockForConfiguration];
        }
        else
        {
            [captureDevice lockForConfiguration:nil];
            [captureDevice setTorchMode: AVCaptureTorchModeOff];
            [captureDevice unlockForConfiguration];
        }
    }
    else
    {
        [[[UIAlertView alloc] initWithTitle:@"温馨提醒"
                                    message:@"抱歉,该设备没有闪光灯而无法使用闪光灯功能!"
                                   delegate:nil
                          cancelButtonTitle:@"确定"
                          otherButtonTitles:nil] show];
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值