如何在UIImagePickerController上访问相机闪光灯? [英] How to access camera flash on UIImagePickerController?
本文介绍了如何在UIImagePickerController上访问相机闪光灯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何使用UIImagePickerController打开iPhone 4上的相机闪光灯。
I would like to know how to switch on the camera flash on the iPhone 4 with UIImagePickerController.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerCameraDeviceFront] == YES)
{
/// What code here ///
}
else
{
NoFlash = [[UIAlertView alloc] initWithTitle:@"Uh-Oh"
message:@"Your device doesn't have a flash camera"
delegate:nil
cancelButtonTitle:@"mhmm, OK"
otherButtonTitles:nil];
NoFlash.delegate = self;
[NoFlash show];
[NoFlash release];
}
}
我已经在这里阅读了UIImagePickerController类参考网页: http://bit.ly/cdAhhB 但我没有找到答案。有人可以帮帮我吗?
I already read the UIImagePickerController Class Reference web page here: http://bit.ly/cdAhhB but I didn't find the answer. Can someone please help me?
谢谢
推荐答案
你可以使用这个。当您想要打开或关闭闪光灯时,基本上可以调用'toggleTorch'。希望这是你想要的。
You can use this. Basically call 'toggleTorch' when you want to turn the flash on or off. Hopefully this is what you were looking for.
- (void) toggleTorch {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
if (device.torchMode == AVCaptureTorchModeOff) {
NSLog(@"It's currently off.. turning on now.");
[power setImage:[UIImage imageNamed:@"on@2x.png"] forState:UIControlStateNormal];
AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
[session addInput:flashInput];
[session addOutput:output];
[device unlockForConfiguration];
[output release];
[session commitConfiguration];
[session startRunning];
[self setTorchSession:session];
[session release];
}
else {
NSLog(@"It's currently on.. turning off now.");
[power.imageView setImage:[UIImage imageNamed:@"off@2x.png"]];
[torchSession stopRunning];
}
}
}
-(IBAction)powerBtn
{
[self toggleTorch];
}
这篇关于如何在UIImagePickerController上访问相机闪光灯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文