ImagePIckerController

图片选取器 : 通常用于照片的选取或者视频的选取 , 继承与 UINavigationController, 所以也可以使用 push pop 的一些视图控制器切换效果 .
iOS
获取图片有三种方法:
  1.
直接调用摄像头拍照 ( 拍照时 , 需要手动将照片保存到本地 , 系统不会自动保存成功后的照片 )
  2.
从相册中选择
  3.
从图库中选择
UIImagePickerController
是系统提供的用来获取图片和视频的接口;
UIImagePickerController 类来获取图片视频,大体分为以下几个步骤:
  1.
初始化 UIImagePickerController 类;
  2.
设置 UIImagePickerController 实例的数据来源类型;
  3.
设置设置代理;
  4.
如果需要做图片修改的话设置 allowsEditing =yes

使用步骤 :
1:
实例化图像选取器对象

UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init]

2:
判断当前设配支持的数据来源
用这些数据来源的时候需要进行检测设备是否支持数据

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



    {
        NSLog(@"
支持相片库 ");
    }

3:
通过上一步获取当前设配支持的数据来源 , 设置我们需要的数据来源
数据来源类型 : 一共三种

enum {
  UIImagePickerControllerSourceTypePhotoLibrary ,//
照片库模式。图像选取控制器以该模式显示时会浏览系统照片库的根目录。

  UIImagePickerControllerSourceTypeCamera ,//
相机模式,图像选取控制器以该模式显示时可以进行拍照或摄像

  UIImagePickerControllerSourceTypeSavedPhotosAlbum //
相机胶卷模式,图像选取控制器以该模式显示时会浏览相机胶卷目录

};



4:
检查支持的媒体来源类型

调用 UIImagePickerController 类的另一个静态方法 availableMediaTypesForSourceType:
返回的是字符串数组, kUTTypeImage 表示静态图片, kUTTypeMovie 表示视频。这两个字符串常量定义在 MobileCoreServices 框架中。


5:
实现代理方法 完成图片的选取
协议方法 :

 
当用户取消完成后调用此方法
  - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
当用户选取完成时调用此方法 , 选取的信息都在 info , info 是一个字典 , 其关键字如下
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info;



调用摄像头来获取资源

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



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;



扩展应用 :
常用属性详解 :

//
指定使用照相机模式 , 可以指定使用相册/照片库
    imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;

 //
设置当拍照完或在相册选完照片后,是否跳到编辑模式进行图片剪裁。只有当 showsCameraControls 属性为 true 时才有效果
    imagepicker.allowsEditing = YES;

canera
相关的属性方法
//
所有含有 cameraXXX 的属性都必须要 sourceType UIImagePickerControllerSourceTypeCamera 时设置才有效果,否则会有异常

 //
设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏
    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);

使用 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.showsCameraControls  = NO;

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

自定义相机拍照画面

   //
设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏
    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;



//
//  ViewController.m
//  ImagePIckerController
//
//  Created by xalo on 15/10/20.
//  Copyright (c) 2015 蓝鸥科技 . All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property ( weak , nonatomic ) IBOutlet UIView *imageView;

@end

@implementation ViewController

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

- (
void )didReceiveMemoryWarning {
    [
super didReceiveMemoryWarning];
   
// Dispose of any resources that can be recreated.
}
- (
IBAction )HandleBtnAction:(UIButton *)sender {
   
    UIActionSheet *sheet ;
   
// 判断是否支持相机
   
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
   
        {
            sheet = [[UIActionSheet alloc] initWithTitle:
@" 选择 " delegate: self cancelButtonTitle: @" 取消 " destructiveButtonTitle: @" 取消 " otherButtonTitles: @" 拍照 " , @" 从相册选择 " , nil ];
        }
    }
   
else
    {
  
        sheet = [[UIActionSheet alloc] initWithTitle:
@" 选择 " delegate: self cancelButtonTitle: nil destructiveButtonTitle: @" 取消 " otherButtonTitles: @" 从相册选择 " , @" 相机胶卷模式 " , nil ];
    }
    [sheet showInView:
self .view];
}

// 判断是否支持相机 , 跳转到相机或者相册界面
- (
void )actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSInteger sourceType =
0 ;
   
   
// 判断是否支持相机
   
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
       
switch (buttonIndex) {
           
case 0 :
               
// 取消
               
break ;
           
case 1 :
               
// 相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
               
break ;
           
case 2 :
               
// 从相册选择
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
               
break ;
           
           
default :
               
break ;
        }
    }
   
else
    {
       
// 当不支持相机时 , 仅支持相册
       
switch (buttonIndex) {
           
case 0 :
               
// 取消
               
break ;
           
case 1 :
               
// 从相册选择
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
               
break ;
           
case 2 :
               
// 从相册胶卷模式
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
               
break ;
           
default :
               
break ;
        }
    }
 
   
// 查看支持的媒体来源类型
    NSLog(
@" 支持的媒体来源 :%@" ,[UIImagePickerController availableMediaTypesForSourceType:sourceType]);
   
   
// 跳转到相机界面
    UIImagePickerController *imagePickerControlelr = [[UIImagePickerController alloc] init];
   
// 设置代理 , 需要制定两个协议 UINavigationControllerDelegate && UIImagePickerControllerDelegate
    imagePickerControlelr.delegate =
self ;
   
// 设置是否允许编辑
    imagePickerControlelr.allowsEditing =
YES ;
   
// 设置打开的数据类型
    imagePickerControlelr.sourceType = sourceType;
   
// 设置媒体来源 ( 相机模式下 )
//    imagePickerControlelr.mediaTypes = @[@"public.image",@"public.movie"];
    imagePickerControlelr.mediaTypes =
@[ @"public.movie" ] ;
  
// imagePickerControlelr.mediaTypes = @[@"public.image"];

   
//imagePickerControlelr.mediaTypes = @[];
   
// 显示当前控制器
    [
self presentViewController:imagePickerControlelr animated: YES completion: nil ];
}

#pragma mark -
#pragma mark - UIImagePickerControllerDelegate -
// 通过实现 imagePickerController 的代理协议 实现 图片的选取

// 完成选取图片时 执行的方法
- (
void )imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   
// 判断当获得的 , 获取当前的媒体类型
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    NSLog(
@" 获取的媒体类型 :%@" , mediaType);
   
   
   
   
// 选取某个图片时 , 退出当前视图控制器
    [picker dismissViewControllerAnimated:
YES completion:^{
        NSLog(
@" 退出照片选取器 " );
    }];
   
// 获取编辑后的图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSLog(
@" 媒体的 URL: %@" ,[info objectForKey:UIImagePickerControllerReferenceURL]);
   
   
/* 此处 info 有六个值
     NSString *const  UIImagePickerControllerMediaType ;
指定用户选择的媒体类型(文章最后进行扩展)
     NSString *const  UIImagePickerControllerOriginalImage ;
原始图片
     NSString *const  UIImagePickerControllerEditedImage ;
修改后的图片
     NSString *const  UIImagePickerControllerCropRect ;
裁剪尺寸
     NSString *const  UIImagePickerControllerMediaURL ;
媒体的 URL
     NSString *const  UIImagePickerControllerReferenceURL ;
原件的 URL
     NSString *const  UIImagePickerControllerMediaMetadata;
当来数据来源是照相机的时候这个值才有效
     */

   
   
// 将图片加载到视图上
    [
self additionImageViewWithImage:image];
   
}
// 按下取消按钮式 执行的方法
- (
void )imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:
YES completion:^{
        NSLog(
@" 关闭相册 " );
    }];
}

#pragma mark - imageView -
- ( void )additionImageViewWithImage:(UIImage *)image
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame =
self .imageView.bounds;
    [
self .imageView addSubview:imageView];
}



@end


   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值