我經歷了相同的問題,因爲你去了。
iOS支持兩種模式 - 閃光燈&火炬。下面的代碼檢查是否每個都可用&然後如果打開或關閉它取決於你打電話給誰。它也會檢查燈是否已經打開/關閉。
強制閃光 -
-(void)flashOn {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
if ([device hasFlash]) {
if ([device flashMode] == AVCaptureFlashModeOff) {
[device setFlashMode:AVCaptureFlashModeOn];
}
}
if ([device hasTorch]) {
if ([device torchMode] == AVCaptureTorchModeOff) {
[device setTorchMode:AVCaptureTorchModeOn];
}
}
[device unlockForConfiguration];
}
}
閃光燈關 -
-(void)flashOff {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
if ([device hasFlash]) {
if ([device flashMode] == AVCaptureFlashModeOn) {
[device setFlashMode:AVCaptureFlashModeOff];
}
}
if ([device hasTorch]) {
if ([device torchMode] == AVCaptureTorchModeOn) {
[device setTorchMode:AVCaptureTorchModeOff];
}
}
[device unlockForConfiguration];
}
}
http://stackoverflow.com/questions/5882829/how-to-turn-the-iphone-camera-flash-on -off – iPatel 2013-03-01 10:08:10