一、屬性
錄像和拍照用到的都是UIImagePickerController這個類,但是使用的時候怎么區分它們呢?下面就先說一下它們的一些基本屬性:
1. 區分拍照或是錄像:cameraCaptureMode:
拍照:UIImagePickerControllerCameraCaptureModePhoto,
錄像:UIImagePickerControllerCameraCaptureModeVideo
默認的是拍照
2.區分是使用攝像頭或是相冊:sourceType
圖庫:UIImagePickerControllerSourceTypePhotoLibrary,
相機:UIImagePickerControllerSourceTypeCamera,
相冊:UIImagePickerControllerSourceTypeSavedPhotosAlbum
3.區分前置攝像頭 后置攝像頭:cameraDevice
后攝像頭:UIImagePickerControllerCameraDeviceRear,
前攝像頭:UIImagePickerControllerCameraDeviceFront
4. 設置是否顯示控制控件:showsCameraControls默認的是顯示控制控件
5.設置拍照:takePicture
6.錄像:
開始:startVideoCapture
結束:stopVideoCapture
7.設置視頻清晰度:videoQuality
UIImagePickerControllerQualityTypeHigh
UIImagePickerControllerQualityTypeMedium
UIImagePickerControllerQualityTypeLow
UIImagePickerControllerQualityType640x480
UIImagePickerControllerQualityTypeIFrame1280x720
UIImagePickerControllerQualityTypeIFrame960x540
8.設置視頻最大的錄像時間:videoMaximumDuration 默認是10分鐘
9. 閃光模式:cameraFlashMode:
UIImagePickerControllerCameraFlashModeOff 關閉的
UIImagePickerControllerCameraFlashModeAuto =0,自動的默認的是自動的
UIImagePickerControllerCameraFlashModeOn 開啟的
10.設置調用攝像頭視圖頁面的 覆蓋視圖:cameraOverlayView
11.設置拍照頁面的形態:cameraViewTransform
二、代理
代理delegate:需要導入兩個代理:
UINavigationControllerDelegate、
UIImagePickerControllerDelegate
@property(nullable,nonatomic,weak) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;
采集完成之后調用 不區分拍照攝像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;
采集取消的時候調用 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
三、代碼示例
廢話不多說了,上代碼(使用的時候請用真機測試,因為模擬器是不支持攝像頭的功能的):
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>//這個框架是選擇媒體類型時需要用到的
#import "UIView Transfrom.h"http://這個是自己封裝的一個簡單的動畫類,這里沒有附上代碼可以將它屏蔽,showTypeLabel的時候用到的
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#define SCREEN_BOUNDS [UIScreen mainScreen].bounds//屏幕尺寸
#define SCREEN_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)//屏幕寬
#define SCREEN_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)//屏幕高
@interface ViewController ()< UINavigationControllerDelegate, UIImagePickerControllerDelegate>{
BOOL isMovie;//判斷拍照攝像的依據
UILabel *showTypeLabel;//顯示拍照或是錄像的控件
UIImageView *showMediaView;//顯示拍照錄像結果的視圖
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UISwitch *mediaSwich = [[UISwitch alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 60, 50, 50)];
[mediaSwich addTarget:self action:@selector(changeMediaType:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mediaSwich];
showTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(mediaSwich.frame), CGRectGetMinY(mediaSwich.frame)+50, 50, 30)];
showTypeLabel.textAlignment = NSTextAlignmentCenter;
showTypeLabel.textColor = [UIColor colorWithRed:0.8963 green:0.3301 blue:1.0 alpha:1.0];
showTypeLabel.text = @"拍照";
[self.view addSubview:showTypeLabel];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
[button setTitle:@"Camera" forState:UIControlStateNormal];
button.backgroundColor = [UIColor brownColor];
[button addTarget:self action:@selector(doit) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
showMediaView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];
showMediaView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:showMediaView];
}
-(void)changeMediaType:(UISwitch *)sender{
[showTypeLabel rotation];
showTypeLabel.text = sender.isOn!=YES?@"拍照":@"錄像" ;
isMovie = sender.isOn;
}
-(void)doit{
UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
//選擇攝像頭設備 默認的是選擇相冊
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
if (isMovie == YES) {
//拍照的時候不選擇mediaTypes不會崩潰是因為默認設置是kUTTypeImage
//kUTTypeImage包含在MobileCoreServices框架里面->需要的內容不是OC里面字符串的類型需要強制轉換
//錄制視頻類型要選擇kUTTypeMovie 它里面包含了audio和video
pickerController.mediaTypes = @[(NSString *)kUTTypeMovie];
//設置錄像 默認的是拍照 -> 選擇cameraCaptureMode一定要先選擇mediaTypes否則會崩潰
pickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
}
//里面包含兩個代理
pickerController.delegate = self;
[self presentViewController:pickerController animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(@"錄制完成");
NSLog(@">>:%@",info);
if ([info[UIImagePickerControllerMediaType]isEqualToString:(NSString *)kUTTypeMovie]) {
NSLog(@"錄像,錄制完成");
NSString *path = (NSString *)[info[UIImagePickerControllerMediaURL] path];
//保存視頻到相冊
UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil) ;
}
if ([info[UIImagePickerControllerMediaType]isEqualToString:(NSString *)kUTTypeImage]) {
NSLog(@"拍照,拍照完成");
UIImage *finishImage = info[UIImagePickerControllerOriginalImage];
showMediaView.image = nil;
showMediaView.image = finishImage;
//將圖片轉換成二進制
NSData *imageData = UIImageJPEGRepresentation(finishImage, 0.1);
NSData *imageData1 = UIImagePNGRepresentation(finishImage);
//把照片保存到相冊
UIImageWriteToSavedPhotosAlbum(finishImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
//視頻保存到相冊之后調用
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:videoPath]];
AVPlayerViewController *vc = [[AVPlayerViewController alloc]init];
vc.player = player;
[self presentViewController:vc animated:YES completion:nil];
}
//保存成功就會調用這個方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
NSLog(@"保存成功");
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"取消");
[self dismissViewControllerAnimated:YES completion:nil];
}
@end