UIImagePickerController Class 概述
2015-02-09 10:01 JG2014 阅读(168) 评论(0) 编辑 收藏 举报不能定制界面,不可派生子类。
使用步骤:
检查制定源是否可用. isSourceTypeAvailable:方法.
检查可用媒体(视频还是只能是图片) availableMediaTypesForSourceType:方法.
设置界面媒体属性mediaTypes property.
显示界面使用presentViewController:animated:completion:方法.iPad中是popover形式.需要确保sourceType有效.
相关操作,移除视图.
有相关代理,有闪光灯支持.
对于视频有10分钟限制,但是可以通过videoMaximumDuration属性更改。可以使用默认的或专用的UIVideoEditorController来编辑视频.
如果向创建一个完全自定义界面的image picker来浏览图片,使用 Assets Library Framework Reference中的类. (AV Foundation Programming Guide 中的 “Media Capture and Access to Camera” )
设置源
+ availableMediaTypesForSourceType: // 指定源可用的媒体种类
+ isSourceTypeAvailable: // 指定源是否在设备上可用
sourceType
// 运行相关接口前需要指明源类型.必须有效,否则抛出异常. picker已经显示的时候改变这个值,picker会相应改变来适应.默认 UIImagePickerControllerSourceTypePhotoLibrary.
设置picker属性
allowsEditing //是否可编辑
delegate
mediaTypes
// 指示picker中显示的媒体类型.设置每种类型之前应用availableMediaTypesForSourceType:检查一下.如果为空或者array中类型都不可用,会发生异常.默认 kUTTypeImage, 只能显示图片.
allowsImageEditing //propertyDeprecated in iOS 3.1
video选取参数
videoQuality //视频拍摄选取时的编码质量.只有mediaTypes包含kUTTypeMovie时有效.
videoMaximumDuration //秒,video最大记录时间,默认10分钟.只用当mediaTypes包含kUTTypeMovie时有效.
自定义界面
showsCameraControls
// 指示 picker 是否显示默认的camera controls.默认是YES,设置成NO隐藏默认的controls来使用自定义的overlay view.(从而可以实现多选而不是选一张picker就dismiss了).只有 UIImagePickerControllerSourceTypeCamera源有效,否则NSInvalidArgumentException异常.
cameraOverlayView
//自定义的用于显示在picker之上的view.只有当源是UIImagePickerControllerSourceTypeCamera时有效.其他时候使用抛出NSInvalidArgumentException异常.
cameraViewTransform
//预先动画.只影响预先图像,对自定义的overlay view和默认的picker无效.只用当picker的源是UIImagePickerControllerSourceTypeCamera时有效,否则NSInvalidArgumentException异常.
选取媒体
– takePicture
//使用摄像头选取一个图片。自定义overlay可以多选。已经有图片正在选取是调用无效,必须要等delegate收到 imagePickerController:didFinishPickingMediaWithInfo:消息后才能再次选取。非UIImagePickerControllerSourceTypeCamera源会导致异常。
– startVideoCapture
– stopVideoCapture
//结束视频选取,之后系统调用delegate的 imagePickerController:didFinishPickingMediaWithInfo:方法。
设置摄像头
cameraDevice //使用的镜头(默认后置的)
+ isCameraDeviceAvailable: // 摄像设备是否可用.
+ availableCaptureModesForCameraDevice: // 设备可用的选取模式
cameraCaptureMode //相机捕获模式
cameraFlashMode //闪光灯模式(默认自动)
+ isFlashAvailableForCameraDevice: // 是否有闪光能力
相关结构
UIImagePickerControllerSourceType
enum {
UIImagePickerControllerSourceTypePhotoLibrary,// 设备的图片库
UIImagePickerControllerSourceTypeCamera,// 设备的摄像头
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 保存的图像
};
typedef NSUInteger UIImagePickerControllerSourceType;
UIImagePickerControllerQualityType
//Video quality settings for movies recorded with the built-in camera, or transcoded by displaying in the image picker.
enum {
UIImagePickerControllerQualityTypeHigh = 0,
UIImagePickerControllerQualityTypeMedium = 1, // default value
UIImagePickerControllerQualityTypeLow = 2,
UIImagePickerControllerQualityType640x480 = 3,
UIImagePickerControllerQualityTypeIFrame1280x720 = 4,
UIImagePickerControllerQualityTypeIFrame960x540 = 5
};
typedef NSUInteger UIImagePickerControllerQualityType;
UIImagePickerControllerCameraDevice
enum {
UIImagePickerControllerCameraDeviceRear,
UIImagePickerControllerCameraDeviceFront
};
typedef NSUInteger UIImagePickerControllerCameraDevice;
UIImagePickerControllerCameraCaptureMode
//The category of media for the camera to capture.
enum {
UIImagePickerControllerCameraCaptureModePhoto,
UIImagePickerControllerCameraCaptureModeVideo
};
typedef NSUInteger UIImagePickerControllerCameraCaptureMode;
UIImagePickerControllerCameraFlashMode
//The flash mode to use with the active camera.
enum {
UIImagePickerControllerCameraFlashModeOff = -1,
UIImagePickerControllerCameraFlashModeAuto = 0,
UIImagePickerControllerCameraFlashModeOn = 1
};
typedef NSInteger UIImagePickerControllerCameraFlashMode;
相关方法确定是否有效 isFlashAvailableForCameraDevice: .
UIImagePickerControllerDelegate
使用UIImageWriteToSavedPhotosAlbum保存图像, UISaveVideoAtPathToSavedPhotosAlbum保存视频. 4.0后使用writeImageToSavedPhotosAlbum:metadata:completionBlock:保存元数据.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//包含选择的图片或者一个视频的URL,详见“Editing Information Keys.”
//如果是设置可编辑属性,那么picker会预显示选中的媒体,编辑后的与初始的都会保存在info中.
– imagePickerControllerDidCancel:
– imagePickerController:didFinishPickingImage:editingInfo://Deprecated in iOS 3.0
Editing Information Keys
NSString *const UIImagePickerControllerMediaType;// 媒体类型
NSString *const UIImagePickerControllerOriginalImage;// 原始未编辑的图像
NSString *const UIImagePickerControllerEditedImage;// 编辑后的图像
NSString *const UIImagePickerControllerCropRect;// 源图像可编辑(有效?)区域
NSString *const UIImagePickerControllerMediaURL;// 视频的路径
NSString *const UIImagePickerControllerReferenceURL;// 原始选择项的URL
NSString *const UIImagePickerControllerMediaMetadata;// 只有在使用摄像头并且是图像类型的时候有效.包含选择图像信息的字典类型
下面代码展示了一些基本操作,由一个按钮事件开始进入picker
-(void) pick
{
[imagePicker takePicture];
}
// 按钮事件
- (IBAction)press:(id)sender {
imagePicker = [UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = NO;
// 自定义界面开始>>>>>>>>>>>>>>
UIView* iview = [UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
iview.backgroundColor = [UIColor redColor];
UIButton* button = [UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(pick) forControlEvents:UIControlEventTouchUpInside];
[iview addSubview:button];
CGRect newFrame = CGRectMake(0.0,436,320,44);
iview.frame = newFrame;
imagePicker.cameraOverlayView =iview;
// <<<<<<<<<<<<<<<<<
[self presentModalViewController:imagePicker animated:YES];
}
// 图片保存回调
- (void) image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo
{
myImageView.image = image;
NSLog(@"%@",error);
}
// 视频保存回调
- (void) video: (NSString *) videoPath
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo
{
NSLog(@"%@",videoPath);
NSLog(@"%@",error);
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// 图片类型
if ([info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]) {
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
// 保存图片
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
// 视频类型 kut什么的定义需要 MobileCoreServices.framework 支持
else if ([info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeMovie]) {
{
NSString* path = [info objectForKey:UIImagePickerControllerMediaURL] path];
// 保存视频
UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}
// 如果想之后立刻调用UIVideoEditor,animated不能是YES。最好的还是dismiss结束后在调用editor。
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}