IOS打开系统相机的闪光灯
IOS有两种的拍照和视频的方式:
1.直接使用UIImagePickerController,这个类提供了一个简单便捷的拍照与选择图片库里图片的功能。
2.另一种是通过AVFoundation.framework框架完全自定义拍照的界面和选择图片库界面。我只做了第一种,就先给大家介绍第一种做法:
一、首先调用接口前,我们需要先判断当前设备是否支持UIImagePickerController,用isSourceTypeAvailable:来判断是否可用
二、查看符合的媒体类型,这个时候我们调用availableMediaTypeForSourceType:判断
在调用UIImagePickerController时我们需要加入他的两个代理方法:
UINavigationControllerDelegate和UIImagePickerControllerDelegate,在调用摄像头的时候还可以调闪光灯,一会代码里有。
要调用闪光灯需要先建一个AVCaptureSession类的实例对象:
// Createdby张茫原on13-1-23.
// Copyright(c)2013年张茫原.Allrightsreserved.
//
#import<UIKit/UIKit.h>
//调用闪光灯调用框架
#import<AVFoundation/AVFoundation.h>
@interfaceCameraViewController:UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
AVCaptureSession*_AVSession;//调用闪光灯的时候创建的类
}
@property(nonatomic,retain)AVCaptureSession*AVSession;
@end
在.m的-(void)viewDidLoad里建立4Button,Camera调用相机、Library调用图片库、flashlight打开闪光灯、close关闭闪光灯,这里创建Button的代码我就不再写了。
//打开相机
-(void)addCarema
{
//判断是否可以打开相机,模拟器此功能无法使用
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController*picker=[[UIImagePickerControlleralloc]init];
picker.delegate=self;
picker.allowsEditing=YES; //是否可编辑
//摄像头
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
[selfpresentModalViewController:pickeranimated:YES];
[pickerrelease];
}else{
//如果没有提示用户
UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"Error"message:@"你没有摄像头"delegate:nilcancelButtonTitle:@"Drat!"otherButtonTitles:nil];
[alertshow];
}
}
打开相机后,然后需要调用UIImagePickerControllerDelegate里的方法,拍摄完成后执行的方法和点击Cancel之后执行的方法:
//拍摄完成后要执行的方法
-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary*)info
{
//得到图片
UIImage*image=[infoobjectForKey:UIImagePickerControllerOriginalImage];
//图片存入相册
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);
[selfdismissModalViewControllerAnimated:YES];
}
//点击Cancel按钮后执行方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
[selfdismissModalViewControllerAnimated:YES];
}
调用相机照片和保存到图片库已经完成。
接着介绍打开照片库:
-(void)openPicLibrary
{
//相册是可以用模拟器打开的
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
UIImagePickerController*picker=[[UIImagePickerControlleralloc]init];
picker.delegate=self;
picker.allowsEditing=YES;//是否可以编辑
//打开相册选择照片
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[selfpresentModalViewController:picker animated:YES];
[pickerrelease];
}else{
UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"Error"message:@"你没有摄像头"delegate:nilcancelButtonTitle:@"Drat!"otherButtonTitles:nil];
[alertshow];
}
}
//选中图片进入的代理方法
-(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingImage:(UIImage*)imageeditingInfo:(NSDictionary*)editingInfo
{
[selfdismissModalViewControllerAnimated:YES];
}
调用闪光灯的代码,由于我也不是很理解,所以没法加注释,但是已经亲测可用,但是调闪光灯时有一个算是bug吧,闪光灯会闲一下,然后再一直亮
-(void)openFlashlight
{
AVCaptureDevice*device=[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
if(device.torchMode==AVCaptureTorchModeOff){
//CreateanAVsession
AVCaptureSession*session=[[AVCaptureSessionalloc]init];
//Createdeviceinputandaddtocurrentsession
AVCaptureDeviceInput*input=[AVCaptureDeviceInputdeviceInputWithDevice:deviceerror:nil];
[sessionaddInput:input];
//Createvideooutputandaddtocurrentsession
AVCaptureVideoDataOutput*output=[[AVCaptureVideoDataOutputalloc]init];
[sessionaddOutput:output];
//Startsessionconfiguration
[sessionbeginConfiguration];
[devicelockForConfiguration:nil];
//Settorchtoon
[devicesetTorchMode:AVCaptureTorchModeOn];
[deviceunlockForConfiguration];
[sessioncommitConfiguration];
//Startthesession
[sessionstartRunning];
//Keepthesessionaround
[selfsetAVSession:self.AVSession];
[outputrelease];
}
}
-(void)closeFlashlight
{
[self.AVSessionstopRunning];
[self.AVSessionrelease];
}
以上所述就是本文的全部内容了,希望大家能够喜欢。