类型
UIImagePickerControllerSourceTypePhotoLibrary 相册 视频
UIImagePickerControllerSourceTypeCamera 拍照 摄像
UIImagePickerControllerSourceTypeSavedPhotosAlbum 时刻
媒体类型
public.movie 视频,拍摄
public.image 相册,拍照
- 相册,拍照
#import "ViewController.h"
@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *photoImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
/** 相册 */
- (IBAction)photo:(id)sender {
// 相册的来源访问UIImagePickerController
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
/*
UIImagePickerControllerSourceTypePhotoLibrary 相册
UIImagePickerControllerSourceTypeCamera 拍照 摄像
UIImagePickerControllerSourceTypeSavedPhotosAlbum 时刻
*/
//数据源类型
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 是否允许编辑
picker.allowsEditing = YES;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
/** 拍照 */
- (IBAction)photograph:(id)sender {
/*
UIImagePickerControllerCameraDeviceRear:后置摄像头
UIImagePickerControllerCameraDeviceFront:前置摄像头
*/
//判断是否有摄像头
BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
if (isCamera) {
UIImagePickerController *imagePickerC = [[UIImagePickerController alloc]init];
//数据源的类型
imagePickerC.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerC.delegate = self;
[self presentViewController:imagePickerC animated:YES completion:nil];
}
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"没有可用的摄像头" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertC addAction:action];
[self presentViewController:alertC animated:YES completion:NULL];
}
//照片保存成功的回调方法 注意:方法名命名有要求
// 存储图片
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
[SVProgressHUD showErrorWithStatus:@"保存失败!"];
} else {
[SVProgressHUD showSuccessWithStatus:@"保存成功!"];
}
}
@end
- 视频, 拍摄
/** 视频 */
- (IBAction)video:(id)sender {
// 相册的来源访问UIImagePickerController
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//数据源类型
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//指定媒体类型 (默认public.image )
picker.mediaTypes = @[@"public.movie"];//public.image
[self presentViewController:picker animated:YES completion:nil];
}
/** 拍摄 */
- (IBAction)shoot:(id)sender {
//判断是否有摄像头
BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
if (isCamera) {
// 相册的来源访问UIImagePickerController
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//数据源类型
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
//指定媒体类型 (默认public.image )
picker.mediaTypes = @[@"public.movie"];//public.image
[self presentViewController:picker animated:YES completion:nil];
}
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"没有可用的摄像头" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertC addAction:action];
[self presentViewController:alertC animated:YES completion:NULL];
}
- 代理 UIImagePickerControllerDelegate
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
//获取媒体类型
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]) { //照片
/*
UIImagePickerControllerEditedImage:编辑后的图片
UIImagePickerControllerOriginalImage:编辑前的图片
*/
self.photoImageView.image = info[@"UIImagePickerControllerEditedImage"];
//如果是照相
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//保存到相册中
UIImageWriteToSavedPhotosAlbum(info[@"UIImagePickerControllerOriginalImage"], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
[picker dismissViewControllerAnimated:YES completion:nil];
} else if ([mediaType isEqualToString:@"public.movie"]) {// 视频
NSURL *url = info[UIImagePickerControllerMediaURL];
// 播放
}
}