很多时候都需要在APP中控制闪光灯的开关状态,譬如扫描二维码。控制iOS设备的闪光灯代码非常简单,短短几行代码就可以搞定:
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error; if (device.hasTorch) { // 判断设备是否有散光灯 BOOL b = [device lockForConfiguration:&error]; if (!b) { if (error) { DLog(@"lock torch configuration error:%@", error.localizedDescription); } return; } device.torchMode = (device.torchMode == AVCaptureTorchModeOff ? AVCaptureTorchModeOn : AVCaptureTorchModeOff); [device unlockForConfiguration]; }
时间: 2024-10-25 01:49:24