開發iOS應用的過程中,很多情景都要調用相機,大多數初學開發者都是采用的蘋果提供的系統相機的方法。
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = sourceType;
[self presentViewController:imagePickerController animated:YES completion:^{}];
頭文件要遵守協議方法,
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
然后在下這個代理方法了里面獲取拍照以后的照片。
//該代理方法僅適用於只選取圖片時
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo
{ NSLog(@"選擇完畢----image:%@-----info:%@",image,editingInfo);
}
使用非常的不方便,而且調用系統的相機是不能自定義相機頁面的,且如果從一個橫屏頁面進入系統的相機,就會崩潰報錯。
因為在做一個自定義相機頁面的時候,遇到了上面所說的坑,所以就有了下面這個demo,我寫的這個相機,采用frame布局,可以隨意自定義拍照頁面,支持橫屏,自定義裁剪。
git地址https://github.com/wubianxiaoxian/SKFCamera
使用方法
在info.plist 里面添加
Privacy - Microphone Usage Description 使用麥克風
Privacy - Camera Usage Description 使用相機
-
Installation with CocoaPods:pod 'SKFCamera',引入
#import <SKFCamera.h>
-
或者下載demo到本地將SKFCamera添加到工程,引入
#import "SKFCamera.h"
-
按照下面的方法引用相機
SKFCamera *homec=[[SKFCamera alloc]init]; __weak typeof(self)myself=self; homec.fininshcapture=^(UIImage *ss){ if (ss) { NSLog(@"照片存在"); //在這里獲取裁剪后的照片 myself.ViewImageview.image=ss; } } ; [self presentViewController:homec animated:NO completion:^{}];}