二. 使用UIImagePickerController指定通常可以分为如下步骤(3-5是拍照或者录制视频的步骤):
- 创建UIImagePickerController对象
- 指定拾取源,平时选择照片时使用的拾取源是照片库、相簿或者摄像头类型
- 指定摄像头,前置摄像头或者后置摄像头
- 设置媒体类型mediaType,注意如果是录像必须设置,如果是拍照此步骤可以省略,因为mediaType默认包含kUUTypeImage(注意媒体类型定义在MobileCoreServices.framework中)
- 指定捕获模式,拍照或者录制视频。(录制视频必须先设置媒体类型在设置捕获模式)
- 展示UIImagePickerController(通常以模态窗口形式打开)
- 拍照和录制视频结束后再代理方法中展示/保存照片或视频
当然这个过程中有很多细节可以设置,例如是否显示拍照控制面板,拍照后是否允许编辑等等,通过上面的属性/方法列表相信并不难理解。
三. 下面是代码:
首先Controller要遵从代理
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
typedef NS_ENUM(NSInteger,ImagePickerType) {
ImagePickerTypePhotoLibrary, // 图集
ImagePickerTypePhotosAlbum, // 照片库
ImagePickerTypeCameraPhoto, // 拍照
ImagePickerTypeCameraVideo // 录像
};
@interface ViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic, assign) ImagePickerType type;
@property (nonatomic, strong) UIImagePickerController *imagePicker;
@property (nonatomic, strong) AVPlayer *player;
@end
按钮的点击事件(获取图片的三种方式)
- (IBAction)selecterdButtonAction:(UIButton *)sender {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"选择照片" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
// 图集
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.type = ImagePickerTypePhotoLibrary;
[self presentViewController:self.imagePicker animated:YES completion:nil];
}];
// 相片库
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相片库" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.type = ImagePickerTypePhotosAlbum;
[self presentViewController:self.imagePicker animated:YES completion:nil];
}];
// 相机
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 判断是否支持相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.type = ImagePickerTypeCameraPhoto;
[self presentViewController:self.imagePicker animated:YES completion:nil];
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"未发现相机" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
}];
// 录制视频
UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"录制视频" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 判断是否支持相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.type = ImagePickerTypeCameraVideo;
[self presentViewController:self.imagePicker animated:YES completion:nil];
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"未发现相机" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
}];
// 取消
UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
}];
[alertVC addAction:action1];
[alertVC addAction:action2];
[alertVC addAction:action3];
[alertVC addAction:action4];
[alertVC addAction:action5];
[self presentViewController:alertVC animated:YES completion:nil];
}
实现代理方法
#pragma mark - UIImagePickerControllerDelegate
// 获取图片的回调方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
NSLog(@"video...");
NSURL *url=[info objectForKey:UIImagePickerControllerMediaURL];//视频路径
NSString *urlStr=[url path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) {
//保存视频到相簿,注意也可以使用ALAssetsLibrary来保存
UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);//保存视频到相簿
}
} else {
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
self.imageView.image = image;
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
// 点击取消按钮的回调方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 私有方法
- (UIImagePickerController *)imagePicker {
if (!_imagePicker) {
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.allowsEditing=YES;//允许编辑
_imagePicker.delegate=self;//设置代理,检测操作
}
return _imagePicker;
}
//视频保存后的回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
if (error) {
NSLog(@"保存视频过程中发生错误,错误信息:%@",error.localizedDescription);
}else{
NSLog(@"视频保存成功.");
//录制完之后自动播放
NSURL *url=[NSURL fileURLWithPath:videoPath];
_player=[AVPlayer playerWithURL:url];
AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:_player];
playerLayer.frame=self.imageView.bounds;
[self.imageView.layer addSublayer:playerLayer];
[_player play];
}
}
- (void)setType:(ImagePickerType)type {
switch (type) {
case ImagePickerTypePhotoLibrary:
{
self.imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
case ImagePickerTypePhotosAlbum:
{
self.imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
break;
}
case ImagePickerTypeCameraPhoto:
{
self.imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.cameraDevice=UIImagePickerControllerCameraDeviceFront;//设置使用哪个摄像头,这里设置为前置摄像头
self.imagePicker.cameraCaptureMode=UIImagePickerControllerCameraCaptureModePhoto;
break;
}
case ImagePickerTypeCameraVideo:
{
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.cameraDevice=UIImagePickerControllerCameraDeviceRear;//设置使用哪个摄像头,这里设置为后置摄像头
self.imagePicker.mediaTypes=@[(NSString *)kUTTypeMovie];
self.imagePicker.videoQuality=UIImagePickerControllerQualityTypeIFrame1280x720;
self.imagePicker.cameraCaptureMode=UIImagePickerControllerCameraCaptureModeVideo;//设置摄像头模式(拍照,录制视频)
break;
}
default:
break;
}
}