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{
if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeImage]) {
//拍照与获取本地相册
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
self.fileData = UIImageJPEGRepresentation(image, 1.0);
self.imageView.image = image;
}else if ([[info objectForKey: UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie]){
//录像与获取本地视频
NSString *videoPath = [info objectForKey:UIImagePickerControllerMediaURL];
self.fileData = [NSData dataWithContentsOfFile:videoPath];
NSLog(@"%@",self.fileData);
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark --Delegate 功能取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}