数据源类型
UIImagePickerControllerSourceTypePhotoLibrary 相 册
UIImagePickerControllerSourceTypeCamera 拍照 摄像
UIImagePickerControllerSourceTypeSavedPhotosAlbum 时刻
媒体类型
mediaTypes:
public.image//是图片类型
public.movie//是视频类型
相册,照相
//相册
- (IBAction)selectImage:(id)sender {
//相册的资源访问UIImagePickerController来读取
UIImagePickerController *imagePickerC = [[UIImagePickerController alloc]init];
//数据源类型
imagePickerC.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;//相册
//是否允许编辑
imagePickerC.allowsEditing = YES;
imagePickerC.delegate = self;
[self presentViewController:imagePickerC animated:YES completion:NULL];
}
//照相
- (IBAction)takePhoto:(id)sender {
/*
UIImagePickerControllerCameraDeviceRear:后置摄像头
UIImagePickerControllerCameraDeviceFront:前置摄像头
*/
//判断是否有摄像头
BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
if (!isCamera) {
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];
return;
}
UIImagePickerController *imagePickerC = [[UIImagePickerController alloc]init];
//数据源的类型
imagePickerC.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerC.delegate = self;
imagePickerC.allowsEditing = YES;
[self presentViewController:imagePickerC animated:YES completion:NULL];
}
视频,拍摄
//视频
- (IBAction)video:(id)sender {
UIImagePickerController *imagePickerC = [[UIImagePickerController alloc]init];
imagePickerC.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerC.delegate = self;
//指定媒体类型 (默认public.image )
imagePickerC.mediaTypes = @[@"public.movie"];//public.image
[self presentViewController:imagePickerC animated:YES completion:NULL];
}
//拍摄
- (IBAction)shoot:(id)sender {
//判断是否有摄像头
BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
if (!isCamera) {
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];
return;
}
UIImagePickerController *imagePickerC = [[UIImagePickerController alloc]init];
//数据流类型:拍摄
imagePickerC.sourceType =UIImagePickerControllerSourceTypeCamera;
imagePickerC.delegate = self;
//指定媒体类型 (默认public.image )
imagePickerC.mediaTypes = @[@"public.movie"];//public.image
[self presentViewController:imagePickerC animated:YES completion:NULL];
}
UIImagePickerController代理方法
#pragma mark - UIImagePickerController Delegate
//完成选择之后
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(@"info = %@",info);
//获取媒体类型
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]) { //照片
/*
UIImagePickerControllerEditedImage:编辑后的图片
UIImagePickerControllerOriginalImage:编辑前的图片
*/
UIImage *img = info[UIImagePickerControllerEditedImage];
self.myImageView.image = img;
//如果是照相
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//保存到相册中
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}else if ([mediaType isEqualToString:@"public.movie"]){ //视频
NSURL *url = info[UIImagePickerControllerMediaURL];
//播放视频 MPMoviePlayerViewController需要引入框架
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
//弹出视频播放器,自动播放
[picker presentViewController:moviePlayer animated:YES completion:NULL];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"取消选择");
[self dismissViewControllerAnimated:YES completion:NULL];
}
//照片保存成功的回调方法 注意:方法名命名有要求
- (void) image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo{
NSLog(@"保存成功");
}
注意:
照相,拍摄是需要摄像头的,因为模拟器没有摄像头,所以需要用真机模拟。
网友评论