IOS SDK詳解之拍照/相冊(默認+自定義拍照界面)

blog.csdn.net/hello_hwc


前言: 
本來要更新NSURLSession的UploadTask的,結果寫那個Demo的時候想要寫成拍照上傳,然後就想到先寫一個關於拍照的Demo吧。本文會先介紹下如何使用系統提供的界面拍照和選擇相冊,然後自定義拍照界面。注意,本文使用的是UIImagePickerController,所以不能完全的自定義,如果想要徹底的自定義拍照,建議選擇AV Foundation這個框架來做


Demo效果 
進入系統的拍照界面 


進入自定義拍照界面 


自定義前置攝像頭和後置攝像頭切換動畫-翻頁 


一 使用系統提供的界面拍照和相冊選擇

第一步 
保存一個UIImagePickerController的實例,然後適當的時候初始化始化。Demo選擇在viewDidLoad初始化。讓當前類實現UIImagePickerControllerDelegate,UINavigationControllerDelegate兩個代理

@property (strong,nonatomic)UIImagePickerController * imagePikerViewController; //初始化 self.imagePikerViewController = [[UIImagePickerController alloc] init]; self.imagePikerViewController.delegate = self;//通過代理來傳遞拍照的圖片 self.imagePikerViewController.allowsEditing = YES;//允許編輯

第二步,通過ActionSheet來讓用戶選擇是拍照還是到相冊選擇,然後模態的顯示

[self presentViewController:self.imagePikerViewController animated:YES completion:NULL];

注意,要先判斷相機是否可用,然後在進入相機(有可能相機壞了,或者在虛擬機上運行的)

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil messagenil preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction: [UIAlertAction actionWithTitle: @"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:self.imagePikerViewController animated:YES completion:NULL]; }else{ [self showAlertWithMessage:@"Camera is not available in this device or simulator"]; } }]]; [alertController addAction: [UIAlertAction actionWithTitle: @"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){ self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:self.imagePikerViewController animated:YES completion:NULL]; } }]]; [alertController addAction: [UIAlertAction actionWithTitle: @"Cancel" style: UIAlertActionStyleCancel handler:nil]]; [self presentViewController: alertController animated: YES completion: nil];

第三部,代理函數處理拍照或者cancel事件

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage * image = info[UIImagePickerControllerEditedImage]; if (!image) { image = info[UIImagePickerControllerOriginalImage]; } self.imageview.image = image; [self dismissViewControllerAnimated:YES completion:NULL]; } -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [self dismissViewControllerAnimated:YES completion:NULL]; }


二 自定義拍照界面

UIImagePickerController的自定義界面比較簡單,通過設置cameraOverlayView這個屬性爲自定義的View就能自定義。 
第一步 創建一個View 
創建xib文件 

拖拽控件,進行autoLayout,最終效果如圖 

把fileOwner設置成CustomTakePhotoViewController 

第二步 在顯示拍照界面之前,把UI設置成自己想要的,注意,把屬性 
showsCameraControls設置爲NO,不讓默認的界面出現。

self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypeCamera; self.imagePikerViewController.showsCameraControls = NO; [[NSBundle mainBundle] loadNibNamed:@"CustomOverLayview" owner:self options:nil]; self.takePictureButton.layer.cornerRadius = 20self.takePictureButton.clipsToBounds = YESself.overlayView.frame = self.imagePikerViewController.cameraOverlayView.frameself.overlayView.backgroundColor = [UIColor clearColor]; self.imagePikerViewController.cameraOverlayView = self.overlayViewself.overlayView = nil; [self presentViewController:self.imagePikerViewController animated:YES completion:NULL];

第三步,爲view上的控件添加動作 
Segment Control負責切換前置攝像頭和後置攝像頭,爲了流暢,在切換的時候顯示動畫。

- (IBAction)cameraSelect:(UISegmentedControl *)sender{ NSInteger index = sender.selectedSegmentIndexif (index == 0) { [UIView transitionWithView:self.imagePikerViewController.view duration:1.0 options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCurlDown animations:^{ self.imagePikerViewController.cameraDevice = UIImagePickerControllerCameraDeviceFront; } completion:NULL]; }else{ [UIView transitionWithView:self.imagePikerViewController.view duration:1.0 options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCurlUp animations:^{ [self.imagePikerViewController setCameraDevice:UIImagePickerControllerCameraDeviceRear]; } completion:NULL]; } }

拍照的Button

- (IBAction)takePicture:(id)sender { [self.imagePikerViewController takePicture]; }

取消的Button

- (IBAction)cancelTakePicture:(id)sender { [self dismissViewControllerAnimated:YES completion:NULL]; }

第四步 在代理中處理圖片

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage * image = info[UIImagePickerControllerEditedImage]; if (!image) { image = info[UIImagePickerControllerOriginalImage]; } self.imageview.image = image; [self dismissViewControllerAnimated:YES completion:NULL]; }


注意,如果log輸出

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

直接忽略就是了,沒有任何影響,貌似是IOS 8的一個bug


下載鏈接 
http://download.csdn.net/detail/hello_hwc/8553539

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章