IOS开发之UIImagePckerController
2016-03-21 17:05
429 查看
建议在看的时候把Screen拉到最宽======让这行文字在一行显示===============================================
1. 注意使用之前必须要先导入UIKit框架
2.使用
创建UIImagePckerController
2.使用流程: |
|
sourceType(选择相册/摄像头)
| 摄像头:UIImagePickerControllerSourceTypeCamera
| 相册:UIImagePickerControllerSourceTypePhotoLibrary
| 相册:UIImagePickerControllerSourceTypeSavedPhotosAlbum
cameraCaptureMode (选择照相/录像) 设置录像 默认拍照----要先选择媒体的类型 不然会崩溃
| 录像:UIImagePickerControllerCameraCaptureModeVideo
| 拍照:UIImagePickerControllerCameraCaptureModePhoto
cameraDevice (前摄像头/后摄像头)
| 前摄像头:UIImagePickerControllerCameraDeviceFront
| 后置摄像头:UIImagePickerControllerCameraDeviceRear
showsCameraControls 是否显示控件
|YES
|NO
takePicture 设置 拍照
|
|
mediaTypes 选择媒体类型
// 拍照的时候不选择媒体类型 不会奔溃 是因为默认设置是KUTTypeImage;
// 需要导入MobileCoreServices框架, MobileCoreServices这个框架中需要的不是oc需要的字符串类型 需要强制转换
// pickcontroller.mediaTypes =@[(NSString *)kUTTypeImage];
|
|
startVideoCapture 设置 录像
| startVideoCapture
| stopVideoCapture
设置清晰度
| 清晰模式
| UIImagePickerControllerQualityTypeLow
| UIImagePickerControllerQualityTypeMedium
| UIImagePickerControllerQualityTypeLow
| 尺寸
| UIImagePickerControllerQualityType640x480
| UIImagePickerControllerQualityTypeIFrame1280x720
| UIImagePickerControllerQualityTypeIFrame960x540
videoMaximumDuration 设置视频最大的录像时间
|
|
cameraFlashMode 设置闪光
| UIImagePickerControllerCameraFlashModeOff = -1,
| UIImagePickerControllerCameraFlashModeAuto = 0,
| UIImagePickerControllerCameraFlashModeOn = 1
cameraViewTransform 设置拍照页面的形态 ( CGAffineTransform)
代理:一共有两个代理: UINavigationControllerDelegate, UIImagePickerControllerDelegate
一个是导航栏代理 一个是pickcontroller代理
// 采集完成之后调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;
// 采集取消的时候调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
// 把图片保存到相册
UIImageWriteToSavedPhotosAlbum(finashImage, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
// 保存照片成功 回调方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
NSLog(@"保存成功");
}
// 视频保存到相册之后调用
UISaveVideoAtPathToSavedPhotosAlbum(info[UIImagePickerControllerMediaURL], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
// 保存视频成功 回调方法
- (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];
}
代码如下:(example)==============================================================
#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];
}
1. 注意使用之前必须要先导入UIKit框架
2.使用
创建UIImagePckerController
2.使用流程: |
|
sourceType(选择相册/摄像头)
| 摄像头:UIImagePickerControllerSourceTypeCamera
| 相册:UIImagePickerControllerSourceTypePhotoLibrary
| 相册:UIImagePickerControllerSourceTypeSavedPhotosAlbum
cameraCaptureMode (选择照相/录像) 设置录像 默认拍照----要先选择媒体的类型 不然会崩溃
| 录像:UIImagePickerControllerCameraCaptureModeVideo
| 拍照:UIImagePickerControllerCameraCaptureModePhoto
cameraDevice (前摄像头/后摄像头)
| 前摄像头:UIImagePickerControllerCameraDeviceFront
| 后置摄像头:UIImagePickerControllerCameraDeviceRear
showsCameraControls 是否显示控件
|YES
|NO
takePicture 设置 拍照
|
|
mediaTypes 选择媒体类型
// 拍照的时候不选择媒体类型 不会奔溃 是因为默认设置是KUTTypeImage;
// 需要导入MobileCoreServices框架, MobileCoreServices这个框架中需要的不是oc需要的字符串类型 需要强制转换
// pickcontroller.mediaTypes =@[(NSString *)kUTTypeImage];
|
|
startVideoCapture 设置 录像
| startVideoCapture
| stopVideoCapture
设置清晰度
| 清晰模式
| UIImagePickerControllerQualityTypeLow
| UIImagePickerControllerQualityTypeMedium
| UIImagePickerControllerQualityTypeLow
| 尺寸
| UIImagePickerControllerQualityType640x480
| UIImagePickerControllerQualityTypeIFrame1280x720
| UIImagePickerControllerQualityTypeIFrame960x540
videoMaximumDuration 设置视频最大的录像时间
|
|
cameraFlashMode 设置闪光
| UIImagePickerControllerCameraFlashModeOff = -1,
| UIImagePickerControllerCameraFlashModeAuto = 0,
| UIImagePickerControllerCameraFlashModeOn = 1
cameraViewTransform 设置拍照页面的形态 ( CGAffineTransform)
代理:一共有两个代理: UINavigationControllerDelegate, UIImagePickerControllerDelegate
一个是导航栏代理 一个是pickcontroller代理
// 采集完成之后调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;
// 采集取消的时候调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
// 把图片保存到相册
UIImageWriteToSavedPhotosAlbum(finashImage, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
// 保存照片成功 回调方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
NSLog(@"保存成功");
}
// 视频保存到相册之后调用
UISaveVideoAtPathToSavedPhotosAlbum(info[UIImagePickerControllerMediaURL], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
// 保存视频成功 回调方法
- (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];
}
代码如下:(example)==============================================================
#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];
}
相关文章推荐
- BlockingQueue
- 如何生成UUID类性的唯一值
- 《iOS Human Interface Guidelines》——Activity Indicator
- iOS设置圆形(圆角)UIView、UIButton的简单方法
- Generic Interfaces (C# Programming Guide)
- [ActionScript&Flex] FlashBuilder编译条件之如何屏蔽调试代码
- soapui脚本中,参数内容为xml文件时候的处理
- UIButton使用误区
- String Stringbuffer Stringbuilder区别
- UIPickView手动选择时动画
- Servlet的Request.getInputStream()只能读取一次
- ndarray.any()--ValueError: 'axis' entry is out of bounds
- iOS 自定义UITableViewCell上添加UIButton按钮如何确定点击了哪一行,代码块实现
- 在使用cocoapods的过程中出现 Use the `$(inherited)` flag, or Remove the build settings from the target.
- HBuilder真机联调iPhone手机设备问题
- 基于easyui开发Web版Activiti流程定制器详解(六)——Draw2d的扩展(三)
- 基于easyui开发Web版Activiti流程定制器详解(六)——Draw2d详解(二)
- Bluetooth
- UICollectionView 与屏幕旋转 问题
- blueGreen develop , 敏捷开发