本文主要是介绍UIImagePicketView(照相机类)的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.挂代理
UIImagePickerControllerDelegate,UINavigationControllerDelegate
2. 看代码UIImagePickerController *imagepicker=[[UIImagePickerController alloc]init];
imagepicker.delegate=self;
// 指定类型: 相机
imagepicker.sourceType=UIImagePickerControllerSourceTypeCamera;
// 拍照完成后是否 进入编辑模式
imagepicker.allowsEditing=YES;
// 在拍照中是否隐藏 编辑按钮(隐藏一般用于自定义摄像机的时候为no)
imagepicker.showsCameraControls = YES;
// 可以指定 前或者后摄像头
imagepicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
/// 指定摄像头的闪光灯的模式
imagepicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
// 定义是否既可以录像换可以拍照片
imagepicker.mediaTypes=[NSArray arrayWithObjects:@"public.movie", @"public.image", nil];
// 使拍照的View旋转 45度
// imagepicker.cameraViewTransform = CGAffineTransformMakeRotation(M_PI*45/180);
// 定义拍照View的缩放比例
// imagepicker.cameraViewTransform=CGAffineTransformMakeScale(1, 2);
[self presentViewController:imagepicker animated:YES completion:^{
}];
浓缩版
UIImagePickerController *imagepicker=[[UIImagePickerController alloc]init];
imagepicker.delegate=self;
// 指定类型: 相机
imagepicker.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagepicker animated:YES completion:^{
}];
//成功获得相片还是视频后的回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//获取图片裁剪的图
UIImage* edit = [info objectForKey:UIImagePickerControllerEditedImage];
//如果是拍照的照片,则需要手动保存到本地(会保存到系统的PhotosAlbum),系统不会自动保存拍照成功后的照片
UIImageWriteToSavedPhotosAlbum(edit, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}else{
}
}
// 上一步 保存照片成功后的会回调函数
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo{
NSLog(@"saved..");
}
//取消按键 照相机的回调
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
//模态方式退出uiimagepickercontroller
[imagepicker dismissModalViewControllerAnimated:YES];
}
4.改中文
1.Project-->Info-->Localizations添加Chinese
二 。详解
1.UIImagePickerController的静态方法:
imagepicker = [[UIImagePickerController alloc]init];
//UIImagePickerController静态方法判断设备是否支持照相机/图片库/相册功能
/*
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
};
*/
BOOL isCameraSupport = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
NSLog(@"support camera:%d",isCameraSupport);
//UIImagePickerController静态方法判断设备是否支持前置摄像头/后置摄像头
/*
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
UIImagePickerControllerCameraDeviceRear,
UIImagePickerControllerCameraDeviceFront
};
*/
BOOL isRearSupport = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
NSLog(@"rear support:%d",isRearSupport);
//UIImagePickerController静态方法判断设备是否支持前置摄像头闪光灯/后置摄像头闪光灯
/*
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
UIImagePickerControllerCameraDeviceRear,
UIImagePickerControllerCameraDeviceFront
};
*/
BOOL isFlushSupport = [UIImagePickerController isFlashAvailableForCameraDevice:UIImagePickerControllerCameraDeviceRear];
NSLog(@"rear flash support:%d",isFlushSupport);
//UIImagePickerController静态方法返回前置摄像头/后置摄像头支持的拍摄类型
/*
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
UIImagePickerControllerCameraDeviceRear,
UIImagePickerControllerCameraDeviceFront
};
*/
//返回类型有照片和视频
/*
enum {
UIImagePickerControllerCameraCaptureModePhoto,
UIImagePickerControllerCameraCaptureModeVideo
};
typedef NSUInteger UIImagePickerControllerCameraCaptureMode;
*/
NSArray* captureModes = [UIImagePickerController availableCaptureModesForCameraDevice:UIImagePickerControllerCameraDeviceRear];
for (NSNumber* mode in captureModes) {
NSLog(@"capture modes:%d",[mode integerValue]);
}
//UIImagePickerController静态方法返回照相机/相册/照片库所支持的媒体类型
/*
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
};
*/
//返回类型有kUTTypeMovie,kUTTypeImage,其他类型均在<MobileCoreServices/MobileCoreServices.h>下
NSArray* mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
for (NSString* type in mediaTypes) {
NSLog(@"media types:%@",type);
}
2.UIImagePickerController属性详解:
//指定使用照相机模式,可以指定使用相册/照片库
imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//设置当拍照完或在相册选完照片后,是否跳到编辑模式进行图片剪裁。只有当showsCameraControls属性为true时才有效果
imagepicker.allowsEditing = YES;
//设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏
imagepicker.showsCameraControls = YES;
//设置使用后置摄像头,可以使用前置摄像头
imagepicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
//设置闪光灯模式
/*
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
UIImagePickerControllerCameraFlashModeOff = -1,
UIImagePickerControllerCameraFlashModeAuto = 0,
UIImagePickerControllerCameraFlashModeOn = 1
};
*/
imagepicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
//设置相机支持的类型,拍照和录像
imagepicker.mediaTypes = @[(NSString*)kUTTypeImage,(NSString*)kUTTypeMovie];
//设置拍摄时屏幕的view的transform属性,可以实现旋转,缩放功能
// imagepicker.cameraViewTransform = CGAffineTransformMakeRotation(M_PI*45/180);
// imagepicker.cameraViewTransform = CGAffineTransformMakeScale(1.5, 1.5);
//所有含有cameraXXX的属性都必须要sourceType是UIImagePickerControllerSourceTypeCamera时设置才有效果,否则会有异常
//设置UIImagePickerController的代理
imagepicker.delegate = self;
-(void)showCamera{
//用模态方式呈现imagepicker
[self presentModalViewController:imagepicker animated:YES];
}
使用 imagepicker.cameraViewTransform = CGAffineTransformMakeRotation(M_PI*45/180);旋转45度的效果:
使用imagepicker.cameraViewTransform = CGAffineTransformMakeScale(1.5, 1.5);全屏的效果,同时imagepicker.showsCameraControls =NO;隐藏工具栏:
使用imagepicker.allowsEditing = YES;出现的图片编辑效果,只有当imagepicker.showsCameraControls = YES;才有效果:
使用 imagepicker.mediaTypes = @[(NSString*)kUTTypeImage,(NSString*)kUTTypeMovie];支持拍照和视频的前后对比效果:
3.UIImagePickerController回调详解:
//成功获得相片还是视频后的回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//通过UIImagePickerControllerMediaType判断返回的是照片还是视频
NSString* type = [info objectForKey:UIImagePickerControllerMediaType];
//如果返回的type等于kUTTypeImage,代表返回的是照片,并且需要判断当前相机使用的sourcetype是拍照还是相册
if ([type isEqualToString:(NSString*)kUTTypeImage]&&picker.sourceType==UIImagePickerControllerSourceTypeCamera) {
//获取照片的原图
UIImage* original = [info objectForKey:UIImagePickerControllerOriginalImage];
//获取图片裁剪的图
UIImage* edit = [info objectForKey:UIImagePickerControllerEditedImage];
//获取图片裁剪后,剩下的图
UIImage* crop = [info objectForKey:UIImagePickerControllerCropRect];
//获取图片的url
NSURL* url = [info objectForKey:UIImagePickerControllerMediaURL];
//获取图片的metadata数据信息
NSDictionary* metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
//如果是拍照的照片,则需要手动保存到本地,系统不会自动保存拍照成功后的照片
UIImageWriteToSavedPhotosAlbum(edit, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}else{
}
//模态方式退出uiimagepickercontroller
[imagepicker dismissModalViewControllerAnimated:YES];
}
//取消照相机的回调
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
//模态方式退出uiimagepickercontroller
[imagepicker dismissModalViewControllerAnimated:YES];
}
//保存照片成功后的回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo{
NSLog(@"saved..");
}
这篇关于UIImagePicketView(照相机类)的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!