iOS设备闪光灯的使用

废话不多说了,直接上代码!

我将其封装为类方法直接调用即可


/** 开或关 闪光灯 */
+ (void)changeFlash {

    //  获取摄像机单例对象
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //必须判定是否有闪光灯,否则如果没有闪光灯会崩溃
    if (![device hasFlash]) return;
    
    //修改前必须先锁定
    [device lockForConfiguration:nil];
    
    if (device.flashMode == AVCaptureFlashModeOff) {
        device.flashMode = AVCaptureFlashModeOn;
        device.torchMode = AVCaptureTorchModeOn;
    } else if (device.flashMode == AVCaptureFlashModeOn) {
        device.flashMode = AVCaptureFlashModeOff;
        device.torchMode = AVCaptureTorchModeOff;
    }
    
    [device unlockForConfiguration];
}

/** 关灯 用于退出时调用 */
+ (void)closeFlash {

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    if (![device hasFlash]) return;
    
    if (device.flashMode == AVCaptureFlashModeOn) {
        
        device.flashMode = AVCaptureFlashModeOff;
        device.torchMode = AVCaptureTorchModeOff;
    }
}

你可能感兴趣的:(iOS设备闪光灯的使用)