您的位置:首页 > 产品设计 > UI/UE

UIImagePickerController 的基本用法 - iOS - UI基础知识总结14

2015-06-11 13:34 846 查看
UIImagePickerController可以通过拍照、图库、相册三种途径获取图片

iOS 获取图片有三种方法:

1、直接调用摄像头拍照

2、从相册中选择

3、从图库中选择

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

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

1、初始化UIImagePickerController 类;

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

3、设置代理;

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

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

<span style="font-size:18px;">enum {
UIImagePickerControllerSourceTypePhotoLibrary ,//来自图库
UIImagePickerControllerSourceTypeCamera ,//来自相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum //来自相册
};</span>


在用这些来源的时候最好检测以下设备是否支持:(否则会造成程序崩溃)

<span style="font-size:18px;">if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
NSLog(@"支持相机");
}
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
NSLog(@"支持图库");
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
NSLog(@"支持相片库");
}</span>


下面通过一个例子介绍获取图库图片的详细步骤

<span style="font-size:18px;">// 设置view的背景颜色
self.view.backgroundColor = [UIColor grayColor];

// 创建UIImageView对象,用来显示从系统相册获取的图片
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height/2)];
// 设置imageView的背景颜色
imageView.backgroundColor = [UIColor whiteColor];
// 设置imageView的边框
[imageView.layer setBorderWidth:3.0];
// 设置边框颜色
[imageView.layer setBorderColor:[UIColor orangeColor].CGColor];

// 给imageView添加点击手势,从而进入系统相册选择图片
// 先打开用户交互
imageView.userInteractionEnabled = YES;
// 创建点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTapAction:)];
[imageView addGestureRecognizer:tap];
[tap release];

// 设置tag值,代理方法会用到imageView
imageView.tag = 111;

[self.view addSubview:imageView];
[imageView release];</span>


点击手势的实现(选取图片)

<span style="font-size:18px;">- (void)imageViewTapAction:(UITapGestureRecognizer *)sender
{
// 创建出系统相册的控制对象
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
// 设置picker的选择源的类型(要选取什么样的东西)
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 设置调用系统相册的modal类型(跳转动画类型)
picker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
// 允许对相册进行编辑
picker.allowsEditing = YES;
// 设置代理
picker.delegate = self;
// 用modal方式跳转到选取图片界面
[self presentViewController:picker animated:YES completion:nil];
// 释放picker
[picker release];
}</span>
// 对于点击手势实现方法里面的modal跳转类型,有以下几种:

typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
UIModalTransitionStyleCoverVertical = 0, 从下往上整页覆盖(返回时从上往下)
UIModalTransitionStyleFlipHorizontal, 门轴试翻转
UIModalTransitionStyleCrossDissolve, 溶解消失
UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2), 上下翻页(跳转时,即往上翻页时最后有卡顿现象,属于系统缺陷)
};


记得要遵守代理并且实现代理方法

<span style="font-size:18px;">#import "RootViewController.h"

@interface RootViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@end</span>


相册代理方法

<span style="font-size:18px;">- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];// 选取编辑过的图片
((UIImageView *)[self.view viewWithTag:111]).image = image;

// 拿到图片后返回到原来界面
[picker dismissViewControllerAnimated:YES completion:nil];
}</span>


对于代理方法中的info,咱们所需的信息全在里面,info是一个字典,根据字典中的键key可以获取对应类型的数据

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


UIImagePickerControllerMediaType 包含着KUTTypeImage 和KUTTypeMovie

KUTTypeImage 包含:

<span style="font-size:18px;">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;</span>


KUTTypeMovie 包含:

<span style="font-size:18px;">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;</span>


参考博客http://www.cocoachina.com/ios/20140923/9730.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: