iOS巔峯之調用系統相機和打開閃光燈

IOS有兩種的拍照和視頻的方式:1.直接使用UIImagePickerController,這個類提供了一個簡單便捷的拍照與選擇圖片庫裏圖片的功能。2.另一種是通過AVFoundation.framework框架完全自定義拍照的界面和選擇圖片庫界面。我只做了第一種,就先給大家介紹第一種做法:
一、首先調用接口前,我們需要先判斷當前設備是否支持UIImagePickerController,用isSourceTypeAvailable:來判斷是否可用
二、查看符合的媒體類型,這個時候我們調用availableMediaTypeForSourceType:判斷
在調用UIImagePickerController時我們需要加入他的兩個代理方法:
UINavigationControllerDelegate和UIImagePickerControllerDelegate,在調用攝像頭的時候還可以調閃光燈,一會代碼裏有。

要調用閃光燈需要先建一個AVCaptureSession類的實例對象:

[java] view plain copy
  1. #import <UIKit/UIKit.h>  
  2. //調用閃光燈調用框架  
  3. #import <AVFoundation/AVFoundation.h>  
  4.    
  5. @interface CameraViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>  
  6. {  
  7.     AVCaptureSession * _AVSession;//調用閃光燈的時候創建的類  
  8. }  
  9.    
  10. @property(nonatomic,retain)AVCaptureSession * AVSession;  
  11.    
  12. @end  
在.m的- (void)viewDidLoad裏建立4Button,Camera調用相機、Library調用圖片庫、flashlight打開閃光燈、close關閉閃光燈

[java] view plain copy
  1. //打開相機  
  2. -(void)addCarema  
  3. {  
  4.     //判斷是否可以打開相機,模擬器此功能無法使用  
  5.     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {  
  6.            
  7.         UIImagePickerController * picker = [[UIImagePickerController alloc]init];  
  8.         picker.delegate = self;  
  9.         picker.allowsEditing = YES;  //是否可編輯  
  10.         //攝像頭  
  11.         picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
  12.         [self presentModalViewController:picker animated:YES];  
  13.         [picker release];  
  14.     }else{  
  15.         //如果沒有提示用戶  
  16.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你沒有攝像頭" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];  
  17.         [alert show];  
  18.     }  
  19. }  

打開相機後,然後需要調用UIImagePickerControllerDelegate裏的方法,拍攝完成後執行的方法和點擊Cancel之後執行的方法:

[java] view plain copy
  1. //拍攝完成後要執行的方法  
  2. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
  3. {  
  4.     //得到圖片  
  5.     UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];  
  6.     //圖片存入相冊  
  7.     UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);  
  8.     [self dismissModalViewControllerAnimated:YES];  
  9.        
  10. }  
  11. //點擊Cancel按鈕後執行方法  
  12. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker  
  13. {  
  14.     [self dismissModalViewControllerAnimated:YES];  
  15. }  


調用相機照片和保存到圖片庫已經完成。
接着介紹打開照片庫:
[java] view plain copy
  1. -(void)openPicLibrary  
  2. {  
  3.     //相冊是可以用模擬器打開的  
  4.     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {  
  5.         UIImagePickerController * picker = [[UIImagePickerController alloc]init];  
  6.         picker.delegate = self;  
  7.         picker.allowsEditing = YES;//是否可以編輯  
  8.    
  9.         //打開相冊選擇照片  
  10.         picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  11.         [self presentModalViewController:picker  animated:YES];  
  12.         [picker release];  
  13.     }else{  
  14.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你沒有攝像頭" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];  
  15.         [alert show];  
  16.     }  
  17.        
  18. }  
  19.    
  20. //選中圖片進入的代理方法  
  21. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo  
  22. {  
  23.     [self dismissModalViewControllerAnimated:YES];  
  24. }  

調用閃光燈的代碼

[java] view plain copy
  1. -(void)openFlashlight  
  2. {  
  3.     AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];  
  4.     if (device.torchMode == AVCaptureTorchModeOff) {  
  5.         //Create an AV session  
  6.         AVCaptureSession * session = [[AVCaptureSession alloc]init];  
  7.            
  8.         // Create device input and add to current session  
  9.         AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];  
  10.         [session addInput:input];  
  11.            
  12.         // Create video output and add to current session   
  13.         AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc]init];  
  14.         [session addOutput:output];  
  15.            
  16.         // Start session configuration  
  17.         [session beginConfiguration];  
  18.         [device lockForConfiguration:nil];  
  19.            
  20.         // Set torch to on  
  21.         [device setTorchMode:AVCaptureTorchModeOn];  
  22.            
  23.         [device unlockForConfiguration];  
  24.         [session commitConfiguration];  
  25.            
  26.         // Start the session  
  27.         [session startRunning];  
  28.            
  29.         // Keep the session around  
  30.         [self setAVSession:self.AVSession];  
  31.            
  32.         [output release];  
  33.     }  
  34. }  
  35.    
  36. -(void)closeFlashlight  
  37. {  
  38.     [self.AVSession stopRunning];  
  39.     [self.AVSession release];  
  40. }  
發佈了64 篇原創文章 · 獲贊 10 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章