Authored by 郁健超

添加闪光灯 review by洛克

... ... @@ -311,7 +311,8 @@
_torch_switch= NO;
_btnTorch = [UIButton buttonWithType:UIButtonTypeCustom];
_btnTorch.frame = CGRectMake(size.width - 4*icon_gap - 4*icon_size, size.height - 10 - icon_size, icon_size, icon_size);
[_btnTorch setImage:[UIImage imageNamed:@"flash_on"] forState:UIControlStateNormal];
[_btnTorch setImage:[UIImage imageNamed:@"flash_off"] forState:UIControlStateNormal];
[_btnTorch setImage:[UIImage imageNamed:@"flash_on"] forState:UIControlStateSelected];
[_btnTorch addTarget:self action:@selector(clickTorch:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btnTorch];
... ... @@ -614,18 +615,28 @@
-(void) clickTorch:(UIButton*) btn
{
if (_txLivePublisher) {
btn.selected = !btn.selected;
_torch_switch = !_torch_switch;
if (![_txLivePublisher toggleTorch:_torch_switch]) {
_torch_switch = !_torch_switch;
[self toastTip:@"闪光灯启动失败"];
}
if (_torch_switch == YES) {
[_btnTorch setImage:[UIImage imageNamed:@"flash_off"] forState:UIControlStateNormal];
}
else
{
[_btnTorch setImage:[UIImage imageNamed:@"flash_on"] forState:UIControlStateNormal];
// if (![_txLivePublisher toggleTorch:_torch_switch]) {
// _torch_switch = !_torch_switch;
// [self toastTip:@"闪光灯启动失败"];
// }
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (_torch_switch) {
if ([device hasTorch]) {
[device lockForConfiguration:nil];
[device setTorchMode: AVCaptureTorchModeOn];
[device unlockForConfiguration];
}else {
[self toastTip:@"闪光灯启动失败"];
}
}else {
if ([device hasTorch]) {
[device lockForConfiguration:nil];
[device setTorchMode: AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
}
}
}
... ...