zoukankan      html  css  js  c++  java
  • UIImagePickerController Class

    概述

    不能定制界面,不可派生子类。

    使用步骤:

    1. 检查制定源是否可用. isSourceTypeAvailable:方法.
    2. 检查可用媒体(视频还是只能是图片) availableMediaTypesForSourceType:方法.
    3. 设置界面媒体属性 mediaTypes property.
    4. 显示界面使用 presentViewController:animated:completion: 方法.iPad中是 popover形式. 需要确保sourceType有效
    5. 相关操作,移除视图.

    有相关代理,有闪光灯支持. 

    对于视频有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

    /*

    开始视频选取。返回NO可能是

    -已经开始选取视频

    -设备不支持

    -设备空间不足

    默认一次只能选取一个,自定义overlay 可以多选。

    有视频已经在选取了时调用这个无效.需要先调用stopVideoCapture,等 delegate 收到 imagePickerController:didFinishPickingMediaWithInfo: 消息,才能开始选取其他的.

    UIImagePickerControllerSourceTypeCamera源调用会抛出 NSInvalidArgumentException 异常.

    更多功能,见 AV Foundation framework.

    */

    • – stopVideoCapture

    // 结束视频选取,之后系统调用delegateimagePickerController: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];
    }

    官方也有相关例子
    http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Introduction/Introduction.html

  • 相关阅读:
    centos7上修改lv逻辑卷的大小
    centos6上调整lv逻辑卷
    nginx的日志配置
    修改Linux系统默认编辑器
    mysqldump命令的安装
    centos7上设置中文字符集
    nginx的80端口跳转到443
    ubuntu上安装docker和docker-compose
    javascript递归、循环、迭代、遍历和枚举概念
    Lattice 开发工具Diamond 相关版本下载地址
  • 原文地址:https://www.cnblogs.com/v2m_/p/2251194.html
Copyright © 2011-2022 走看看