码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2018-09-19 01:24:02      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:对象   设置图   coding   front   wan   atp   相机   cal   照相机   

      UIImagePickerController 是一个管理系统多媒体文件库(相册)中的图片、视频文件的视图控制器,诞生于iOS4之前,虽然功能不是很完善,我们仍可以用这个视图控制器做一些有创造性的开发,接下来会对其的常见和主要的使用逐个介绍。  

 首先 贴上一张图,帮助我们了解UIImagePickerController的整个构成,以及相关API被设计出来的用途: 

技术分享图片

  UIImagePickerController管理用户在使用相机或者相簿时的交互,并且将交互产生的图片、视频文件传送给其delegate对象。交互产生的文件类型是图片还是视频取决于对使用其进行交互前对资源类型的设置,我们可以通过设置 UIImagePickerControllerSourceType的值来决定。详情参考官方文档的描述:

UIImagePickerControllerSourceType是一个枚举选项,如下typedef enum UIImagePickerControllerSourceType : NSInteger {
    UIImagePickerControllerSourceTypePhotoLibrary, 
    UIImagePickerControllerSourceTypeCamera,
    UIImagePickerControllerSourceTypeSavedPhotosAlbum
} UIImagePickerControllerSourceType;

对其相关描述如下:
UIImagePickerControllerSourceTypePhotoLibrary

Specifies the device’s photo library as the source for the image picker controller.

使用相册 作为 image picker controller的资源文件打开

UIImagePickerControllerSourceTypeCamera

Specifies the device’s built-in camera as the source for the image picker controller. Indicate the specific camera you want (front or rear, as available) by using the cameraDevice property.

使用摄像头进行操作 获取资源文件

UIImagePickerControllerSourceTypeSavedPhotosAlbum

Specifies the device’s Camera Roll album as the source for the image picker controller. If the device does not have a camera, specifies the Saved Photos album as the source

使用通过摄像头处理得到的资源文件作为展示对象,若设备不支持摄像头,则使用用户通过其他途径获取到的文件作为展示对象。

 

在使用UIImagePickerController操作时,我们不仅要设置sourceType,还可以设置allowsEdicting、allowImageEditing等属性。

//UIImagePickerController常见用途 

1. 调用摄像头拍照

2. 从相册中选择

3. 从图库中选择

UIImagePickerController 是系统提供的用来获取图片和视频的接口;

用UIImagePickerController 类来获取图片视频,大体分为以下几个步骤:

1. 初始化UIImagePickerController 类;

2. 设置UIImagePickerController 实例的数据来源类型(下面解释);

3. 设置设置代理;

4. 如果需要做图片修改的话设置allowsEditing =yes。

数据来源类型一共有三种:

enum {
   UIImagePickerControllerSourceTypePhotoLibrary ,//来自图库
   UIImagePickerControllerSourceTypeCamera ,//来自相机
   UIImagePickerControllerSourceTypeSavedPhotosAlbum //来自相册
};

在用这些来源的时候最好检测以下设备是否支持;

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        NSLog(@"支持相机");
    }
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        NSLog(@"支持图库");
    }
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        NSLog(@"支持相片库");
    }

 


调用摄像头来获取资源

- (void)viewDidLoad {
    [super viewDidLoad];
    picker = [[UIImagePickerController alloc]init];
    picker.view.backgroundColor = [UIColor orangeColor];
    UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
    picker.sourceType = sourcheType;
    picker.delegate = self;
    picker.allowsEditing = YES;
}

 


上面只是实例了UIImagePickerController及其属性 在需要获取图片的时候需要弹出窗口调用

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

 


我们还需要代理来获取我们选中的图片

UIImagePickerControllerDelegate

代理中一共三个方法 其中一个3.0 已经废弃了,只剩下两个我们需要用的

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

 


当用户选取完成后调用;

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

 


当用户取消选取时调用;

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

 


选取的信息都在info中,info 是一个字典。

字典中的键:

NSString *const  UIImagePickerControllerMediaType ;指定用户选择的媒体类型(文章最后进行扩展)
NSString *const  UIImagePickerControllerOriginalImage ;原始图片
NSString *const  UIImagePickerControllerEditedImage ;修改后的图片
NSString *const  UIImagePickerControllerCropRect ;裁剪尺寸
NSString *const  UIImagePickerControllerMediaURL ;媒体的URL
NSString *const  UIImagePickerControllerReferenceURL ;原件的URL
NSString *const  UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效

 


UIImagePickerController 的更多参数参考这里

代理中的功能参考这里

UIImagePickerControllerMediaType 包含着KUTTypeImage 和KUTTypeMovie

KUTTypeImage 包含:

const CFStringRef  kUTTypeImage ;抽象的图片类型
const CFStringRef  kUTTypeJPEG ;
const CFStringRef  kUTTypeJPEG2000 ;
const CFStringRef  kUTTypeTIFF ;
const CFStringRef  kUTTypePICT ;
const CFStringRef  kUTTypeGIF ;
const CFStringRef  kUTTypePNG ;
const CFStringRef  kUTTypeQuickTimeImage ;
const CFStringRef  kUTTypeAppleICNS 
const CFStringRef kUTTypeBMP;
const CFStringRef  kUTTypeICO;

 


KUTTypeMovie 包含:

const CFStringRef  kUTTypeAudiovisualContent ;抽象的声音视频
const CFStringRef  kUTTypeMovie ;抽象的媒体格式(声音和视频)
const CFStringRef  kUTTypeVideo ;只有视频没有声音
const CFStringRef  kUTTypeAudio ;只有声音没有视频
const CFStringRef  kUTTypeQuickTimeMovie ;
const CFStringRef  kUTTypeMPEG ;
const CFStringRef  kUTTypeMPEG4 ;
const CFStringRef  kUTTypeMP3 ;
const CFStringRef  kUTTypeMPEG4Audio ;
const CFStringRef  kUTTypeAppleProtectedMPEG4Audio

 

使用UIImagePickerController工作的步骤如下:

 

1.检查媒体来源模式是否可用

 

2.检查该来源模式下所支持的媒体类型

 

3.创建图像选取控制器,设置其属性并显示

 

4.在委托协议方法中处理交互结果得到的资源
 
 
现在马上进入实战进行体验一把:
在一个控制器上添加如下代码
- (IBAction)takePictureButtonClick:(id)sender{
    //检查相机模式是否可用
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSLog(@"sorry, no camera or camera is unavailable.");
        return;
    }
    //获得相机模式下支持的媒体类型
    NSArray* availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    BOOL canTakePicture = NO;
    for (NSString* mediaType in availableMediaTypes) {
        if ([mediaType isEqualToString:[NSString stringWithCString:kUTTypeImage encoding:NSUTF8StringEncoding]]) {
            //支持拍照
            canTakePicture = YES;
            break;
        }
    }
    //检查是否支持拍照
    if (!canTakePicture) {
        NSLog(@"sorry, taking picture is not supported.");
        return;
    }
    //创建图像选取控制器
    UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
    //设置图像选取控制器的来源模式为相机模式
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    //设置图像选取控制器的类型为静态图像
    imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeImage, nil];
    //允许用户进行编辑
    imagePickerController.allowsEditing = YES;
    //设置委托对象
    imagePickerController.delegate = self;
    //以模视图控制器的形式显示
    [self presentModalViewController:imagePickerController animated:YES];
}

- (IBAction)captureVideoButtonClick:(id)sender{
    //检查相机模式是否可用
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSLog(@"sorry, no camera or camera is unavailable!!!");
        return;
    }
    //获得相机模式下支持的媒体类型
    NSArray* availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    BOOL canTakeVideo = NO;
    for (NSString* mediaType in availableMediaTypes) {
        if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
            //支持摄像
            canTakeVideo = YES;
            break;
        }
    }
    //检查是否支持摄像
    if (!canTakeVideo) {
        NSLog(@"sorry, capturing video is not supported.!!!");
        return;
    }
    //创建图像选取控制器
    UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
    //设置图像选取控制器的来源模式为相机模式
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    //设置图像选取控制器的类型为动态图像
    imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeMovie, nil];
    //设置摄像图像品质
    imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
    //设置最长摄像时间
    imagePickerController.videoMaximumDuration = 30;
    //允许用户进行编辑
    imagePickerController.allowsEditing = YES;
    //设置委托对象
    imagePickerController.delegate = self;
    //以模式视图控制器的形式显示
    [self presentModalViewController:imagePickerController animated:YES];
    
}

- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{
    if (!error) {
        NSLog(@"picture saved with no error.");
    }
    else
    {
        NSLog(@"error occured while saving the picture%@", error);
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //打印出字典中的内容
    NSLog(@"get the media info: %@", info);
    //获取媒体类型
    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    //判断是静态图像还是视频
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        //获取用户编辑之后的图像
        UIImage* editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
        //将该图像保存到媒体库中
        UIImageWriteToSavedPhotosAlbum(editedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        //获取视频文件的url
        NSURL* mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
        //创建ALAssetsLibrary对象并将视频保存到媒体库
        ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
        [assetsLibrary writeVideoAtPathToSavedPhotosAlbum:mediaURL completionBlock:^(NSURL *assetURL, NSError *error) {
            if (!error) {
                NSLog(@"captured video saved with no error.");
            }else
            {
                NSLog(@"error occured while saving the video:%@", error);
            }
        }];
    }
    [picker dismissModalViewControllerAnimated:YES];
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

 

 


 

 

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

标签:对象   设置图   coding   front   wan   atp   相机   cal   照相机   

原文地址:https://www.cnblogs.com/CoderAlex/p/9672258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!