天天看點

IOS系統照相機的調用

IOS開發調用系統相機和打開閃光燈

     今天給大家分享一下如何調用iphone的拍照功能和打開閃光燈,有些代碼我也不太了解,很多是在網上借鑒其他人的。IOS有兩種的拍照和視訊的方式:1.直接使用UIImagePickerController,這個類提供了一個簡單便捷的拍照與選擇圖檔庫裡圖檔的功能。2.另一種是通過AVFoundation.framework架構完全自定義拍照的界面和選擇圖檔庫界面。我隻做了第一種,就先給大家介紹第一種做法:

一、首先調用接口前,我們需要先判斷目前裝置是否支援UIImagePickerController,用isSourceTypeAvailable:來判斷是否可用

二、檢視符合的媒體類型,這個時候我們調用availableMediaTypeForSou rceType:判斷

在調用UIImagePickerController時我們需要加入他的兩個代理方法:

UINavigationControllerDe legate和UIImagePickerControllerD elegate,在調用攝像頭的時候還可以調閃光燈,一會代碼裡有。

要調用閃光燈需要先建一個AVCaptureSession類的執行個體對象:

//

//   Created by Mason on 13-1-23.

//   Copyright (c) 2013年 Mason. All rights reserved.

//

#import

//調用閃光燈調用架構

#import

@interface CameraViewController : UIViewController

{

       AVCaptureSession * _AVSession;//調用閃光燈的時候建立的類

}

@property(nonatomic,retain)AVCaptureSession * AVSession;

@end

在.m的- (void)viewDidLoad裡建立4Button,Camera調用相機、Library調用圖檔庫、flashlight打開閃光燈、close關閉閃光燈

//打開相機

-(void)addCarema

{

       //判斷是否可以打開相機,模拟器此功能無法使用

       if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerS ourceTypeCamera]) {

               UIImagePickerController * picker = [[UIImagePickerController alloc]init];

               picker.delegate = self;

               picker.allowsEditing = YES;   //是否可編輯

               //攝像頭

               picker.sourceType = UIImagePickerControllerS ourceTypeCamera;

               [self presentModalViewControll er:picker animated:YES];

               [picker release];

       }else{

               //如果沒有提示使用者

               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你沒有攝像頭" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];

               [alert show];

       }

}

打開相機後,然後需要調用UIImagePickerControllerD elegate裡的方法,拍攝完成後執行的方法和點選Cancel之後執行的方法:

//拍攝完成後要執行的方法

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWit hInfo:(NSDictionary *)info

{

       //得到圖檔

       UIImage * image = [info objectForKey:UIImagePickerControllerO riginalImage];

       //圖檔存入相冊

       UIImageWriteToSavedPhoto sAlbum(image, nil, nil, nil);

       [self dismissModalViewControll erAnimated:YES];

}

//點選Cancel按鈕後執行方法

-(void)imagePickerControllerDid Cancel:(UIImagePickerController *)picker

{

       [self dismissModalViewControll erAnimated:YES];

}

調用相機照片和儲存到圖檔庫已經完成。

接着介紹打開照片庫:

-(void)openPicLibrary

{

       //相冊是可以用模拟器打開的

       if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerS ourceTypePhotoLibrary]) {

               UIImagePickerController * picker = [[UIImagePickerController alloc]init];

               picker.delegate = self;

               picker.allowsEditing = YES;//是否可以編輯

               //打開相冊選擇照片

               picker.sourceType = UIImagePickerControllerS ourceTypePhotoLibrary;

               [self presentModalViewControll er: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 dismissModalViewControll erAnimated:YES];

}

調用閃光燈的代碼,由于我也不是很了解,是以沒法加注釋,但是已經親測可用,但是調閃光燈時有一個算是bug吧,閃光燈會閑一下,然後再一直亮

-(void)openFlashlight

{

       AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaTy pe:AVMediaTypeVideo];

       if (device.torchMode == AVCaptureTorchModeOff) {

               //Create an AV session

               AVCaptureSession * session = [[AVCaptureSession alloc]init];

               // Create device input and add to current session

               AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

               [session addInput:input];

               // Create video output and add to current session

               AVCaptureVideoDataOutput  * output = [[AVCaptureVideoDataOutput  alloc]init];

               [session addOutput:output];

               // Start session configuration

               [session beginConfiguration];

               [device lockForConfiguration:nil];

               // Set torch to on

               [device setTorchMode:AVCaptureTorchModeOn];

               [device unlockForConfiguration];

               [session commitConfiguration];

               // Start the session

               [session startRunning];

               // Keep the session around

               [self setAVSession:self.AVSession];

               [output release];

       }

}

-(void)closeFlashlight

{

       [self.AVSession stopRunning];

       [self.AVSession release];

}