IOS开发之UIImagePckerController
建议在看的时候把Screen拉到最宽======让这行文字在一行显示===============================================
1. 注意使用之前必须要先导入UIKit框架
2.使用
创建UIImagePckerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;
// 采集取消的时候调用
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
NSLog(@"保存成功");
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:videoPath]];
AVPlayerViewController *avplayercon = [[AVPlayerViewController alloc]init];
avplayercon.player = player;
[self presentViewController:avplayercon animated:YES completion:nil];
#define SCREEN_BOUNDS [UIScreen mainScreen].bounds//屏幕尺寸
#define SCREEN_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)//屏幕宽
#define SCREEN_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)//屏幕高
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>{
UIImagePickerController *pickcontroller;
UILabel *showTypeLable;
BOOL isMovie;//判断摄像拍照的依据
UIImageView *imageView;//显示拍照录像结果的视图
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
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];
//UICollectionView
UISwitch *mediaSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-100, 50, 50, 50)];
[mediaSwitch addTarget:self action:@selector(changeMediaType:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mediaSwitch];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 150, 100, 100)];
[self.view addSubview:imageView];
showTypeLable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(mediaSwitch.frame), CGRectGetMinY(mediaSwitch.frame)-30, 50, 20)];
showTypeLable.textAlignment = NSTextAlignmentCenter;
showTypeLable.textColor = [UIColor colorWithRed:0.0078 green:0.6396 blue:1.0 alpha:1.0];
showTypeLable.text = @"拍照";
[self.view addSubview:showTypeLable];
}
-(void)changeMediaType:(UISwitch *)sender{
showTypeLable.text = sender.isOn!=YES?@"拍照":@"录像";
if (isMovie==YES) {
// 设置录像 默认拍照----要先选择媒体的类型
// *******不然会崩溃
pickcontroller.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
}
}
-(void)doit{
pickcontroller = [[UIImagePickerController alloc]init];
// 选择摄像头设备
// 默认的是相册
pickcontroller.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:pickcontroller animated:YES completion:nil];
// 选择媒体类型
// 拍照的时候不选择媒体类型 不会奔溃 是因为默认设置是KUTTypeImage;
// MobileCoreServices这个框架中需要的不是oc需要的字符串类型 需要强制转换
// pickcontroller.mediaTypes =@[(NSString *)kUTTypeImage];
pickcontroller.delegate = self;
// 设置录像 默认拍照----要先选择媒体的类型
// *******不然会崩溃
pickcontroller.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
if ([info [UIImagePickerControllerMediaType]isEqualToString:(NSString *)kUTTypeMovie]) {
NSLog(@"w拍完了");
NSLog(@"====%@",info);
// 视频保存到相册之后调用
UISaveVideoAtPathToSavedPhotosAlbum(info[UIImagePickerControllerMediaURL], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
UIImage *finashImage = info[UIImagePickerControllerOriginalImage];
imageView.image = nil;
imageView.image =finashImage;
// NSData *imageData =UIImageJPEGRepresentation(finashImage, 0.1); //传二进制图片进服务器
// UIImagePNGRepresentation(finashImage);// 传png格式的照片进服务器
// 把图片保存到相册
UIImageWriteToSavedPhotosAlbum(finashImage, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
}
}
//保存照片成功 回调方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
NSLog(@"保存成功");
}
//保存视频成功 回调方法
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:videoPath]];
AVPlayerViewController *avplayercon = [[AVPlayerViewController alloc]init];
avplayercon.player = player;
[self presentViewController:avplayercon animated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"取消");
[self dismissViewControllerAnimated:YES completion:nil];
}