IOS UIImagePickerController从拍照、图库、相册获取图片
iOS 获取图片有三种方法:
1. 直接调用摄像头拍照
2. 从相册中选择
3. 从图库中选择
UIImagePickerController 是系统提供的用来获取图片和视频的接口;
用UIImagePickerController 类来获取图片视频,大体分为以下几个步骤:
1. 初始化UIImagePickerController 类;
2. 设置UIImagePickerController 实例的数据来源类型(下面解释);
3. 设置设置代理;
4. 如果需要做图片修改的话设置allowsEditing =yes。
数据来源类型一共有三种:
1
2
3
4
5
6
7
8
9
|
enum {
UIImagePickerControllerSourceTypePhotoLibrary , //来自图库
UIImagePickerControllerSourceTypeCamera , //来自相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum //来自相册
};
|
在用这些来源的时候最好检测以下设备是否支持;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
NSLog(@ "支持相机" );
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
NSLog(@ "支持图库" );
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
NSLog(@ "支持相片库" );
}
|
调用摄像头来获取资源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
- ( 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及其属性 在需要获取图片的时候需要弹出窗口调用
1
|
[self presentViewController:picker animated:YES completion:nil];
|
我们还需要代理来获取我们选中的图片
1
|
UIImagePickerControllerDelegate
|
代理中一共三个方法 其中一个3.0 已经废弃了,只剩下两个我们需要用的
1
2
3
|
- ( void )imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
*)info;
|
当用户选取完成后调用;
1
|
- ( void )imagePickerControllerDidCancel:(UIImagePickerController *)picker;
|
当用户取消选取时调用;
1
2
3
|
- ( void )imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info;
|
选取的信息都在info中,info 是一个字典。
字典中的键:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
NSString * const UIImagePickerControllerMediaType ;指定用户选择的媒体类型(文章最后进行扩展)
NSString * const UIImagePickerControllerOriginalImage ;原始图片
NSString * const UIImagePickerControllerEditedImage ;修改后的图片
NSString * const UIImagePickerControllerCropRect ;裁剪尺寸
NSString * const UIImagePickerControllerMediaURL ;媒体的URL
NSString * const UIImagePickerControllerReferenceURL ;原件的URL
NSString * const UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效
|
UIImagePickerControllerMediaType 包含着KUTTypeImage 和KUTTypeMovie
KUTTypeImage 包含:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 包含:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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;
|
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/yishengzhiai005/article/details/46717453