iOS learning - calling the system flashlight

In the developer documentation, the flashlight has three states, which are off, on, and automatic:

typedef NS_ENUM(NSInteger, AVCaptureFlashMode) {
    AVCaptureFlashModeOff  = 0,
    AVCaptureFlashModeOn   = 1,
    AVCaptureFlashModeAuto = 2,
} API_AVAILABLE(macos(10.7), ios(4.0), macCatalyst(14.0)) API_UNAVAILABLE(tvos) API_UNAVAILABLE(watchos);

AVCaptureTorchModeOff indicates that the flashlight is off, AVCaptureTorchModeOn indicates that the flashlight is on, and AVCaptureTorchModeAuto indicates that the flashlight is automatically used according to the ambient light conditions.

Instructions:

//开启+关闭手电筒
-(void)setFlashlight{
    AVCaptureDevice *device =[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //修改前必须先锁定
    [device lockForConfiguration:nil];
    //必须判定是否有闪光灯,否则如果没有闪光灯会崩溃
    if([device hasFlash]) {
        if(device.torchMode==AVCaptureFlashModeOff) {
            device.torchMode=AVCaptureTorchModeOn;
        }else if(device.torchMode==AVCaptureFlashModeOn) {
            device.torchMode=AVCaptureTorchModeOff;
        }
    }
    [device unlockForConfiguration];
}

Guess you like

Origin blog.csdn.net/qq_43441647/article/details/130504407