美文网首页图像音频
UIImagePickerController 的使用

UIImagePickerController 的使用

作者: MeteorCode | 来源:发表于2017-02-17 11:12 被阅读905次

IOS获取图片有三种方式:

- UIImagePickerControllerSourceTypePhotoLibrary 相册

- UIImagePickerControllerSourceTypeCamera 相机

- UIImagePickerControllerSourceTypeSavedPhotosAlbum 图库

最好先判断设备数据来源是否支持

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

}

步骤:

第一步:选择获取图片的方式

UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选取", nil];

[sheet showInView:self.view];

第二部:创建UIImagePickerController

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

NSUInteger sourceType = UIImagePickerControllerSourceTypeCamera;

switch (buttonIndex) {

case 0: //相机

sourceType = UIImagePickerControllerSourceTypeCamera;

break;

case 1: //相册

sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

break;

case 2: //取消

return;

break;

default:

break;

}

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

imagePicker.delegate = self;

imagePicker.sourceType = sourceType; //图片来源

imagePicker.showsCameraControls  = YES;// 设置是否显示相机原生 UI 默认yes

imagePicker.allowsEditing = YES; //允许剪切图片

imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront; //前置摄像头

[self presentViewController:imagePicker animated:YES completion:nil];

}

第三步:两个delegate方法 

确定使用相片

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {

[picker dismissViewControllerAnimated:YES completion:nil];

UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];//使用剪切后的图片

self.authenPhotoView.imageView.image = image;

}

取消

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:nil];

}

知识补充:

1 - didFinishPickingMediaWithInfo 中的 info 可以选择使用的图片:

UIImagePickerControllerMediaType  --  指定用户选择的媒体类型(文章最后进行扩展)

UIImagePickerControllerOriginalImage -- 原始图片

UIImagePickerControllerEditedImage -- 修改后的图片

UIImagePickerControllerCropRect -- 裁剪尺寸

UIImagePickerControllerMediaURL -- 媒体的URL

UIImagePickerControllerReferenceURL -- 原件的URL

UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效

UIImage *image = [info objectForKey: UIImagePickerControllerEditedImage ];

2 -  摄像头 -> cameraDevice

UIImagePickerControllerCameraDeviceRear -- 后置摄像头

UIImagePickerControllerCameraDeviceFront -- 前置摄像头

可以判断摄像头是否支持

if ([UIImagePickerController isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceRear]) {

}

3 - 设置相机的两种模式 -> cameraCaptureMode

UIImagePickerControllerCameraCaptureModePhoto -- 照相模式,默认

UIImagePickerControllerCameraCaptureModeVideo -- 录像模式

imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;

4 - 媒体类型 -> mediaTypes

需要 #import才能用 kUTTypeImage 和 KUTTypeMovie 。

UIImagePickerControllerMediaType 包含着KUTTypeImage 和KUTTypeMovie ;默认kUTTypeImage;

imagePickerController.mediaTypes = @[(NSString *)kUTTypeImage, (NSString *)kUTTypeMovie];

4.1 - kUTTypeImage 包含:           

 kUTTypeImage -- 抽象的图片类型         

  kUTTypeJPEG          

kUTTypeJPEG2000          

kUTTypeTIFF         

 kUTTypePICT           

 kUTTypeGIF        

 kUTTypePNG         

 kUTTypeQuickTimeImage          

kUTTypeAppleICNS       

kUTTypeBMP      

kUTTypeICO        

4.2 - kUTTypeMovie 包含:            

kUTTypeAudiovisualContent -- 抽象的声音视频           

 kUTTypeMovie -- 抽象的媒体格式(声音和视频)            

kUTTypeVideo -- 只有视频没有声音           

 kUTTypeAudio -- 只有声音没有视频            

kUTTypeQuickTimeMovie           

kUTTypeMPEG         

 kUTTypeMPEG4          

kUTTypeMP3         

 kUTTypeMPEG4Audio         

kUTTypeAppleProtectedMPEG4Audio  

5 - 设置闪光灯模式 -> cameraFlashMode

UIImagePickerControllerCameraFlashModeOff  = -1  // 关

UIImagePickerControllerCameraFlashModeAuto = 0   // 自动,默认

UIImagePickerControllerCameraFlashModeOn  = 1    // 开

imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;

6 - 设置录像质量 -> videoQuality

UIImagePickerControllerQualityTypeHigh = 0     // 高质量,highest quality

UIImagePickerControllerQualityTypeMedium = 1   // 中质量,默认,Wi-Fi

UIImagePickerControllerQualityTypeLow = 2    // 低质量,cellular network

UIImagePickerControllerQualityType640x480 = 3   // VGA 质量,VGA quality

UIImagePickerControllerQualityTypeIFrame1280x720 = 4 

UIImagePickerControllerQualityTypeIFrame960x540 = 5

imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;

7 - 录像

[imagePickerController startVideoCapture];   // 开始录像

[imagePickerController stopVideoCapture];   // 停止录像

imagePickerController.videoMaximumDuration = 600;   // 设置录像时间长度 default is 10 minutes. apply only mediaTypes includes kUTTypeMovie

8 - 自定义相机视图

imagePickerController.showsCameraControls = YES;   //设置是否显示相机原生 UI ;默认使用,为no时才可以自定义相机视图

imagePickerController.cameraOverlayView = myView;     // 自定义相机视图

imagePickerController.cameraViewTransform = CGAffineTransformMakeRotation(0.25 * M_PI);   //改变相机视图 - 旋转 45 度,需要输入的参数为弧度,45/180 * M_PI,1 度 = PI/180 弧度;

参考:http://www.cnblogs.com/QianChia/p/5758377.html

相关文章

网友评论

    本文标题:UIImagePickerController 的使用

    本文链接:https://www.haomeiwen.com/subject/uupkwttx.html