转载自:http://blog.csdn.net/kingsley_cxz/article/details/9157093

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..");
}

4.自定义相机拍照画面:

//设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏
    imagepicker.showsCameraControls  = NO;

UIToolbar* tool = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-55, self.view.frame.size.width, 75)];
    tool.barStyle = UIBarStyleBlackTranslucent;
    UIBarButtonItem* cancel = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelCamera)];
    UIBarButtonItem* add = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(savePhoto)];
    [tool setItems:[NSArray arrayWithObjects:cancel,add, nil]];
   //把自定义的view设置到imagepickercontroller的overlay属性中

imagepicker.cameraOverlayView = tool;

-(void)cancelCamera{
    [imagepicker dismissModalViewControllerAnimated:YES];
}
-(void)savePhoto{
    //拍照,会自动回调- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info,对于自定义照相机界面,拍照后回调可以不退出实现连续拍照效果
    [imagepicker takePicture];
}

5.UIImagePickerController是继承UINavigationController,所以可以push和pop一些viewcontroller进行导航效果。例如,自定义照相机画面的时候可以在拍摄完后push一个viewcontroller用于对照片进行编辑。

 

- (void)viewDidLoad

{

[super viewDidLoad];

// 生成调用系统相册的按钮

UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];

button.frame = CGRectMake(20,64,200,40);

button.backgroundColor = [UIColor orangeColor];

[button setTitle:  @"添加图片" forState: UIControlStateNormal];

[button addTarget: self action: @selector(showPicker) forControlEvents: UIControlEventTouchUpInside];

[self.view addSubview: button];

// 创建imageView

m_imageView = [[UIImageView alloc] initWithImage: nil];

m_imageView.backgroundColor = [UIColor redColor];

m_imageView.frame = CGRectMake(0,150,320,300);

[self.view addSubview: m_imageView];

}

- (void)showPicker

{

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

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  // 设置类型

picker.delegate = self;

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

}

#pragma mark delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)aImage editingInfo:(NSDictionary *)editingInfo

{

// 将imageView设置为已选的照片

m_imageView.image = aImage;

[picker dismissViewControllerAnimated:YES completion:nil];

}

UIImagePickerController 操作图库的更多相关文章

  1. iOS-系统 图片、视频 管理控制器UIImagePickerController

        UIImagePickerController 是一个管理系统多媒体文件库(相册)中的图片.视频文件的视图控制器,诞生于iOS4之前,虽然功能不是很完善,我们仍可以用这个视图控制器做一些有创造 ...

  2. 【Android开发】监听图库数据库的变化

    步骤一: 保存图片或者删除之前,初始化ContentObserver ScreenshotContentObserver mScreenObserver = new ScreenshotContent ...

  3. iOS中怎么存储照片到自定义相册

    在市场上主流App中,大多数App都具有存储图片到自己App的相册中.苹果提供的方法只能存储图片到系统相册,下面讲一下怎么实现: 实现思路:  1.对系统相册进行操作的前提必须导入#import &l ...

  4. iOS摄像头和相册-UIImagePickerController常用操作

    在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频.并且从相册中选取我们需要的图片或者视频. 关于iOS摄像头和相册的应用,可以使用UIImagePickerController类来完成控制. ...

  5. UIImagePickerController从拍照、图库、相册获取图片

    iOS 获取图片有三种方法: 1. 直接调用摄像头拍照 2. 从相册中选择 3. 从图库中选择 UIImagePickerController 是系统提供的用来获取图片和视频的接口: 用UIImage ...

  6. UIImagePickerController 视频录制操作,视频大小,时间长度

    一:使用 iOS 系统 UIImagePickerController 获取视频大小 获取视频长度 - (void)viewDidLoad { [super viewDidLoad]; // Do a ...

  7. altium designer 中器件原理图库中,将一个器件分成几部分是如何操作的?就是如何用part表示?

    在SCH Library的Components中选中你要添加part的器件,tools菜单--new part即可

  8. iOS系统相册的有关操作

    iOS中,我们选择相册中的资源和调用摄像头可以使用 :UIImagePickerController类来完成,不使用UI我们可以通过:ALAssetsLibrary类来使用相册资源. 一. ALAss ...

  9. Google Maps API V3 之绘图库 信息窗口

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

随机推荐

  1. HDOJ(HDU) 2103 Family planning(需要注意范围)

    Problem Description As far as we known,there are so many people in this world,expecially in china.Bu ...

  2. 《算法问题实战策略》-chaper17-部分和

    数组上的一个基本优化——部分和: 对于一定长度的数组,我们想不断访问这个数组上的某个区间的和,我们能够怎么做呢?这里先不去谈一些数据结构在这个问题上的优化处理.首先我们最简单的一个方法就是穷举出所有区 ...

  3. F - Rain on your Parade - hdu 2389(二分图匹配,Hk算法)

    题意:给一些人和一些伞的坐标,然后每个人都有一定的速度,还有多少时间就会下雨,问最多能有多少人可以拿到伞. 分析:题意很明确,可以用每个人和伞判断一下是否能够达到,如果能就建立一个联系.不过这道题的数 ...

  4. linux下ssh使用和配置

    常用格式:ssh [-l login_name] [-p port] [user@]hostname 更详细的可以用ssh -h查看. 举例 不指定用户: ssh 192.168.0.11 指定用户: ...

  5. Windows 如何在cmd命令行中查看、修改、删除与添加环境变量

    转自:http://www.cnblogs.com/saptechnique/archive/2013/02/17/2914222.html 首先明确一点: 所有的在cmd命令行下对环境变量的修改只对 ...

  6. 简单使用NSURLConnection、NSURLRequest和NSURL

    以下是代码,凝视也写得比較清楚: 头文件须要实现协议NSURLConnectionDelegate和NSURLConnectionDataDelegate // // HttpDemo.h // My ...

  7. java web mvc思想介绍

    1.首先简介一下什么是MVC思想. 在百度百科里面对MVC的说明,MVC全名是Model View Controller.是模型(model)-视图(view)-控制器(controller)的缩写. ...

  8. 【iOS问题记录】关于UITableViewCell的高度、填充

    创建了继承自UITableViewCell的类,在创建该类的同时创建了.xib文件,在cell中填充UIImageView,其frame根据cell的frame调整.在.m中添加以下方法: -(id) ...

  9. 《UNIX网络编程》之read_timeout实验

    最近在做项目,需要做一个服务器和客户端的基于TCP的套接口网络编程,由于服务器端返回数据并不是那么的及时,因此,需要在客户端做些延迟,然后才能去读取数据,实验测试结果如下. 首先,我们先来看一下我们封 ...

  10. Android(java)学习笔记255:JNI之JNI概念

    1. JNI是什么? java native interface (java本机接口) 比如方法声明: public final native Class<?>  getClass(): ...