imagePicker
1,检测设备是否安装摄像头或者是否允许操作相册
(BOOL)[UIImagePickerController isScourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotoAlbum];
有以下可选
UIImagePickerControllerSourceTypePhotoLibrary,// 设备的图片 UIImagePickerControllerSourceTypeCamera,// 设备的摄像头 UIImagePickerControllerSourceTypeSavedPhotosAlbum // 保存的图像
2,设置数据源类型
UIImagePickerController *imagePicker = [UIImagePickerController alloc]init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotoAlbum;
3,设置代理,并且代理需要遵循两个协议<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
imagePicker.delegate = self;
4,将选择器推进来
[ self presentViewController:imagePicker animated:NO];
5, 设置支持的类型,可以只支持图片,可以只支持摄像,可以两者都支持
NSString *requiredMediaType = ( NSString *)kUTTypeImage;
NSString *requiredMediaType1 = ( NSString *)kUTTypeMovie;
NSArray *arrMediaTypes=[NSArray arrayWithObjects:requiredMediaType, requiredMediaType1,nil];
[imagePicker setMediaTypes:arrMediaTypes];
实现代理方法
1,- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES];
}
2,- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];//编辑后的图片,可以选原图
[imagePicker dismissViewControllerAnimated:YES completion:nil];
}