IOS开发调用系统相机和打开闪光灯
今天给大家分享一下如何调用iphone的拍照功能和打开闪光灯,有些代码我也不太理解,很多是在网上借鉴其他人的。IOS有两种的拍照和视频的方
式:1.直接使用UIImagePickerController,这个类提供了一个简单便捷的拍照与选择图片库里图片的功能。2.另一种是通过
AVFoundation.framework框架完全自定义拍照的界面和选择图片库界面。我只做了第一种,就先给大家介绍第一种做法:
一、首先调用接口前,我们需要先判断当前设备是否支持UIImagePickerController,用isSourceTypeAvailable:来判断是否可用
二、查看符合的媒体类型,这个时候我们调用availableMediaTypeForSourceType:判断
在调用UIImagePickerController时我们需要加入他的两个代理方法:
UINavigationControllerDelegate和UIImagePickerControllerDelegate,在调用摄像头的时候还可以调闪光灯,一会代码里有。
要调用闪光灯需要先建一个AVCaptureSession类的实例对象:
- #import <UIKit/UIKit.h>
- #import <AVFoundation/AVFoundation.h>
-
- @interface CameraViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
- {
- AVCaptureSession * _AVSession;
- }
-
- @property(nonatomic,retain)AVCaptureSession * AVSession;
-
- @end
在.m的- (void)viewDidLoad里建立4Button,Camera调用相机、Library调用图片库、flashlight打开闪光灯、close关闭闪光灯
- -(void)addCarema
- {
-
- if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
-
- UIImagePickerController * picker = [[UIImagePickerController alloc]init];
- picker.delegate = self;
- picker.allowsEditing = YES;
-
- picker.sourceType = UIImagePickerControllerSourceTypeCamera;
- [self presentModalViewController:picker animated:YES];
- [picker release];
- }else{
-
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
- [alert show];
- }
- }
打开相机后,然后需要调用UIImagePickerControllerDelegate里的方法,拍摄完成后执行的方法和点击Cancel之后执行的方法:
- -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- {
-
- UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
-
- UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
- [self dismissModalViewControllerAnimated:YES];
-
- }
- -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
- {
- [self dismissModalViewControllerAnimated:YES];
- }
调用相机照片和保存到图片库已经完成。
接着介绍打开照片库:
- -(void)openPicLibrary
- {
-
- if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
- UIImagePickerController * picker = [[UIImagePickerController alloc]init];
- picker.delegate = self;
- picker.allowsEditing = YES;
-
-
- picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- [self presentModalViewController:picker animated:YES];
- [picker release];
- }else{
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
- [alert show];
- }
-
- }
-
- -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
- {
- [self dismissModalViewControllerAnimated:YES];
- }
调用闪光灯的代码
- -(void)openFlashlight
- {
- AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
- if (device.torchMode == AVCaptureTorchModeOff) {
-
- AVCaptureSession * session = [[AVCaptureSession alloc]init];
-
-
- AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
- [session addInput:input];
-
-
- AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc]init];
- [session addOutput:output];
-
-
- [session beginConfiguration];
- [device lockForConfiguration:nil];
-
-
- [device setTorchMode:AVCaptureTorchModeOn];
-
- [device unlockForConfiguration];
- [session commitConfiguration];
-
-
- [session startRunning];
-
-
- [self setAVSession:self.AVSession];
-
- [output release];
- }
- }
-
- -(void)closeFlashlight
- {
- [self.AVSession stopRunning];
- [self.AVSession release];
- }
IOS开发调用系统相机和打开闪光灯
原文:http://www.cnblogs.com/huangh/p/4218632.html