//遵循代理
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
//设置变量
UIImagePickerController *_imagePickerController;//调取系统相机、相册
#pragma mark - 相机、相册相关
//创建_imagePickerController
- (void) createImagePickerController {
if (!_imagePickerController) {
_imagePickerController = [[UIImagePickerController alloc] init];
_imagePickerController.delegate = self;
_imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
_imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie];//图片是:kUTTypeImage
_imagePickerController.allowsEditing = YES;
}
}
//从摄像头获取视频(或图片)
- (void)selectImageFromCamera
{
_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//下面是video需要设置的属性
_imagePickerController.videoMaximumDuration = 30;//录制视频时长,默认10s
_imagePickerController.videoQuality = UIImagePickerControllerQualityTypeMedium;//视频上传质量
_imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;//设置摄像头模式(拍照,录制视频)为录像模式
[self presentViewController:_imagePickerController animated:YES completion:nil];
}
//从相册获取视频(或图片)
- (void)selectImageFromAlbum
{
_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_imagePickerController animated:YES completion:nil];
}
//获取资源成功代理方法(适用获取所有媒体资源,只需判断资源类型)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
//判断资源类型
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
//如果是图片
UIImage * image = info[UIImagePickerControllerEditedImage];
//压缩图片
NSData *fileData = UIImageJPEGRepresentation(image, 1.0);
//上传图片
//[self uploadImageWithData:fileData];
}else{
//如果是视频
NSURL *url = info[UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:url];
//视频上传
//[self uploadVideoWithData:videoData];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
//“取消”操作代理方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
参考文章:
https://www.jianshu.com/p/e70a184d1f32
网友评论