UIImagePickerController拍照/相册/录像/本地视频
1.导入系统库
#import <MobileCoreServices/MobileCoreServices.h>
2.遵守协议
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
3.创建
#pragma mark 相机--拍照
- (void)openCamera{
UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
ipc.allowsEditing = YES;
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
#pragma mark 录像
- (void)openVideo{
UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeMovie, nil];
ipc.videoQuality = UIImagePickerControllerQualityTypeHigh;
ipc.allowsEditing = YES ;
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
#pragma mark 相册
- (void)openPhoto{
UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
ipc.allowsEditing = YES;
ipc.delegate = self;
[self presentViewController: ipc animated:YES completion:nil ];
}
#pragma mark 本地视频
- (void)openVideoList{
UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeMovie, nil];
ipc.allowsEditing = YES;
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
4.代理方法
#pragma mark --Delegate 拍完后执行
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//判断是照片 or 视频
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage] && picker.sourceType == UIImagePickerControllerSourceTypeCamera ) {
//图片
UIImage *theImage = nil;
//判断照片是否允许修改
if ([picker allowsEditing]) {
//获取编辑后的照片
theImage = [info objectForKey:UIImagePickerControllerEditedImage];
}else{
theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
}
//保存至相册
UIImageWriteToSavedPhotosAlbum(theImage, self, nil, nil);
}else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){
//视频
//获取视频url
NSURL *mediaUrl = [info objectForKey:UIImagePickerControllerMediaURL];
//保存
//方式一:
//保存视频至相册
NSString *urlPath = [mediaUrl path];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlPath)) {
UISaveVideoAtPathToSavedPhotosAlbum(urlPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}
});
//方式二:
//创建 ALAssetLibrary对象,并保存到媒体看
// ALAssetsLibrary *assetsLibray = [[ALAssetsLibrary alloc]init];
// [assetsLibray writeVideoAtPathToSavedPhotosAlbum:mediaUrl completionBlock:^(NSURL *assetURL, NSError *error) {
// if (!error) {
// NSLog(@"保存成功");
// }else{
// NSLog(@"保存失败%@",error);
// }
//
// }];
}
//隐藏
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark --Delegate 功能取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}