一、相册、相机、摄像的调用
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h> //添加静态库头文件
@interface ViewController ()<UIImagePickerControllerDelegate> //遵守协议并实现协议方法
@property (nonatomic, strong) UIImagePickerController * picker; //控制器
@property (nonatomic, strong) UIImageView * imageView; //显示相册或拍照的图片
@property (nonatomic, strong) AVPlayer * player; //用于显示动画
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self getPhotoBtn];
[self getCamera];
[self showPhoto];
}
-(void)getPhotoBtn{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 70, 200, 100);
btn.backgroundColor = [UIColor lightGrayColor];
[btn setTitle:@"相册" forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(fromPhotoLibrary) forControlEvents:UIControlEventTouchUpInside];
}
-(void)getCamera{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 200, 200, 100);
btn.backgroundColor = [UIColor lightGrayColor];
[btn setTitle:@"相机" forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(fromCamera) forControlEvents:UIControlEventTouchUpInside];
}
-(void)showPhoto{
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 320, 200, 200)];
imageView.backgroundColor = [UIColor redColor];
_imageView = imageView;
[self.view addSubview:imageView];
}
/*
sourceType:用来确定用户界面显示的样式
@property(nonatomic) UIImagePickerControllerSourceType sourceType
enum {
UIImagePickerControllerSourceTypePhotoLibrary,//显示图库
UIImagePickerControllerSourceTypeCamera,//显示相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum//显示moment图片
};
typedef NSUInteger UIImagePickerControllerSourceType;
*/
// 调起手机相册
- (void)fromPhotoLibrary
{
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = (id)self;
//1、默认选项是:UIImagePickerControllerSourceTypePhotoLibrary
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//2、针对_picker.sourceType返回所有可用的mediaTypes.
_picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
//3、是否允许对获得的图片进行编辑,default value is NO.
_picker.allowsEditing = YES;
//4、视频最大的时间长度 default value is 10 minutes.
_picker.videoMaximumDuration = 100.f; //100 seconds
[self presentViewController:_picker animated:YES completion:nil];
}
// 调起拍照功能
- (void)fromCamera
{
//相机功能是否可用,调用相机
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
//相机访问权限问题
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0) return;
//Available in iOS 7.0 and later.
AVAuthorizationStatus authstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authstatus ==AVAuthorizationStatusDenied){
//用户关闭了权限
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"用户关闭了权限" delegate:self cancelButtonTitle:NSLocalizedString(@"OK",@"确定")otherButtonTitles:nil,nil];
alertView.delegate =self;
[alertView show];
}
else if (authstatus ==AVAuthorizationStatusRestricted){
//The user is not allowed to access media capture devices.
}
else if (authstatus ==AVAuthorizationStatusNotDetermined){
//Explicit user permission is required for media capture, but the user has not yet granted or denied such permission.
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted){
}
else{
}
}];
}
else if (authstatus ==AVAuthorizationStatusAuthorized){
//The user has explicitly granted permission for media capture, or explicit user permission is not necessary for the media type in question.
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = (id)self;
//1、默认选项是:UIImagePickerControllerSourceTypePhotoLibrary
_picker.sourceType = UIImagePickerControllerSourceTypeCamera;
#if 0
//2-1、针对_picker.sourceType返回所有可用的mediaTypes.
_picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
/*
打印这个数组可以看到下边的结果
mediaTypes = (
"public.image",
"public.movie"
)
*/
NSLog(@"mediaTypes = %@",_picker.mediaTypes);
#elif 0
//2-2、所以还可以这么设置
// _picker.mediaTypes = [NSArray arrayWithObject:@"public.image"];
#elif 1
//2-3、主要是下边的两能数,@"public.movie", @"public.image" 一个是录像,一个是拍照
_picker.mediaTypes = [NSArray arrayWithObjects:@"public.image",@"public.movie", nil];
#endif
//3、是否允许对获得的图片进行编辑,default value is NO.
_picker.allowsEditing = YES;
//4、显示相机的所有控件 默认YES
_picker.showsCameraControls = YES;
//5、set a view to overlay the preview view类似相框
_picker.cameraOverlayView = nil;
//6、设定图像缩放
_picker.cameraViewTransform = CGAffineTransformScale(_picker.cameraViewTransform, 1.0, 1.0);
//7、拍摄照片的清晰度,只有在照相机模式下可用
/*
拍摄照片的清晰度,只有在照相机模式下可用
enum {
UIImagePickerControllerQualityTypeHigh = 0, // 高质量
UIImagePickerControllerQualityType640x480 = 3, // VGA quality
UIImagePickerControllerQualityTypeMedium = 1, // 中质量,适合于wifi传输
UIImagePickerControllerQualityTypeLow = 2 // 低质量,适合于蜂窝数据传输
};
typedef NSUInteger UIImagePickerControllerQualityType;
@property(nonatomic) UIImagePickerControllerQualityType videoQuality //默认选中的是UIImagePickerControllerQualityTypeMedium
*/
_picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
//8、设置照相模式还是摄像模式
/*
可以设置照相机的模式,照相还是录视频,默认照相模式。
enum {
UIImagePickerControllerCameraCaptureModePhoto,//照相模式 默认模式
UIImagePickerControllerCameraCaptureModeVideo//摄像模式
};
typedef NSUInteger UIImagePickerControllerCameraCaptureMode;
@property(nonatomic) UIImagePickerControllerCameraCaptureMode cameraCaptureMode
*/
_picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
//9、使用哪个摄像头
/*
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
UIImagePickerControllerCameraDeviceRear,//后置摄像头
UIImagePickerControllerCameraDeviceFront//前置摄像头
} __TVOS_PROHIBITED;
*/
_picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
//10、设置拍照时闪光灯模式
/*
enum {
UIImagePickerControllerCameraFlashModeOff = -1, //关闭
UIImagePickerControllerCameraFlashModeAuto = 0, //自动
UIImagePickerControllerCameraFlashModeOn = 1 //打开
};
typedef NSInteger UIImagePickerControllerCameraFlashMode;
@property(nonatomic) UIImagePickerControllerCameraFlashMode cameraFlashMode
*/
_picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
[self presentViewController:_picker animated:YES completion:^(){}];
}
}
else{
//如果没有相机访问功能,就进行提示
NSLog(@"没有相机功能");
}
}
#pragma mark--点击相册中的图片 货照相机照完后点击use 后触发的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
/* info dictionary keys
UIImagePickerControllerOriginalImage // a UIImage
UIImagePickerControllerEditedImage // a UIImage
UIImagePickerControllerMediaURL // an NSURL
*/
//获取修改后的图片
UIImage * originalImage = info[UIImagePickerControllerOriginalImage];
UIImage * editedImg = info[UIImagePickerControllerEditedImage];
NSURL * mediaUrl = info[UIImagePickerControllerMediaURL];
//选中图片进行了裁剪
if (editedImg) {
self.imageView.image = editedImg;
}
else{
//没有对图片进行裁剪
self.imageView.image = originalImage;
}
//如果选中的是视频
if (mediaUrl) {
self.imageView.image = nil;
[self creatMediaPlayerWithUrl:mediaUrl];
}
//移除图片选择的控制器
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark--点击cancel 调用的方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"取消了操作");
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark--创建播放器
-(void)creatMediaPlayerWithUrl:(NSURL * )url{
// 创建播放器
AVPlayer *player = [AVPlayer playerWithURL:url];
[player play];
// 创建播放器图层
AVPlayerLayer *layer = [AVPlayerLayer layer];
layer.player = player;
layer.frame = self.imageView.bounds;
// 添加图层到控制器的view
[self.imageView.layer addSublayer:layer];
self.player = player;
}
@end
参考文章:
1、UIImagePickerController使用 - dean19900504的专栏 - 博客频道 - CSDN.NET
2、
3、