iOS 获取图片(拍照,图库,相册)

时间:2022-09-18 23:07:12

iOS  获取图片有三种方法

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 的更多参数参考http://blog.sina.com.cn/s/blog_68edaff101019ppe.html

代理中得功能参考 http://bbs.9ria.com/forum.php?mod=viewthread&tid=244453




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;