-UIImagePickerController
顾名思义,就是用于采集选取图片,有三种模式
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,//从图库中选取
UIImagePickerControllerSourceTypeCamera,//用照相机拍照选取
UIImagePickerControllerSourceTypeSavedPhotosAlbum//从相册中选取
};
每次只能选取一张图片,要想选取多张图片,可以自定义实现。
拍照-demon
开启拍摄视频的模式只需配置属性mediaTypes
imagePickerVC.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
自带的视频采集每次只能采集一段视频,可自定义实现断点、续拍、合成。
#pragma mark - View LifeCircle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark - UIButton Action
-(IBAction)buttonAction:(id)sender
{
if (sender == pictureCapture) {
if ([self checkCameraAuthorization]) {
UIImagePickerController * imagePickerVC = [[UIImagePickerController alloc]init];
imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerVC.delegate = self;
[self presentViewController:imagePickerVC animated:YES completion:^{
}];
}
}
if (sender == videoCapture) {
}
}
#pragma mark Private Method
-(BOOL)checkCameraAuthorization
{
BOOL isAvalible = YES;
//ios 7.0以上的系统新增加摄像头权限检测
if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 7.0) {
//获取对摄像头的访问权限。
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (authStatus) {
case AVAuthorizationStatusRestricted://此应用程序没有被授权访问的照片数据。可能是家长控制权限。
NSLog(@"Restricted");
break;
case AVAuthorizationStatusDenied://用户已经明确否认了这一照片数据的应用程序访问.
NSLog(@"Denied");
isAvalible = NO;
break;
case AVAuthorizationStatusAuthorized://用户已授权应用访问照片数据.
NSLog(@"Authorized");
break;
case AVAuthorizationStatusNotDetermined://用户尚未做出了选择这个应用程序的问候
isAvalible =[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
break;
default:
break;
}
}
if (!isAvalible) {
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"您关闭了搜狐社区的相机权限,无法进行拍照。可以在手机 > 设置 > 隐私 > 相机中开启权限。" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[errorAlert show];
errorAlert = nil;
}
return isAvalible;
}
#pragma mark - UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage * originImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[image setImage:originImage];
UIImageWriteToSavedPhotosAlbum(originImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
//保存图片到图库
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo{
NSLog(@"saved..");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}