IOS SDK詳解之拍照/相簿(預設+自訂拍照介面),iossdk
原創blog,轉載請註明出處
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 message: nil 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 = 20;self.takePictureButton.clipsToBounds = YES;self.overlayView.frame = self.imagePikerViewController.cameraOverlayView.frame;self.overlayView.backgroundColor = [UIColor clearColor];self.imagePikerViewController.cameraOverlayView = self.overlayView;self.overlayView = nil;[self presentViewController:self.imagePikerViewController animated:YES completion:NULL];
第三步,為view上的控制項添加動作
Segment Control負責切換自拍和後置網路攝影機,為了流暢,在切換的時候顯示動畫。
- (IBAction)cameraSelect:(UISegmentedControl *)sender{ NSInteger index = sender.selectedSegmentIndex; if (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