UIImagePickerController——–图片选取器
最后更新于:2022-04-01 06:59:24
@UIImagePickerController 类是获取选择图片和视频的用户接口。我们可以用这个controller选择我们所需要的图片和视频。对于这个类来说比较特殊,我们不能够任意定制,也不可以继承生成子类。
1.当前设备是否支持使用UIImagePickerController,需要调用
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
2.查看符合的媒体类型(图片或者视频,调用availableMediaTypesForSourceType: 方法判断。
3.sourceType(从哪选取媒体)
// 打开图片库根目录选择
UIImagePickerControllerSourceTypePhotoLibrary
// 使用相机选择
UIImagePickerControllerSourceTypeCamera
// 打开SavedPhoto目录选择
UIImagePickerControllerSourceTypeSavedPhotosAlbum
4.先创建一个UIImagePickerController(可以用模态推出相册或UIActionSheet推出)
~~~
@实现协议UIImagePickerControllerDelegate
_imagePickerC = [[UIImagePickerController alloc] init];
_imagePickerC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_imagePickerC.allowsEditing = YES;
_imagePickerC.delegate = self;
[self presentModalViewController: _imagePickerC animated: YES];
~~~
allowEditing 设置为YES,表示 允许用户编辑图片,否则,不允许用户编辑。
5.两个主要的委托方法
~~~
// 用户选中图片之后的回调
- (void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
{
if (picker == picker_camera_)
{
//如果是 来自照相机的image,那么先保存
UIImage* original_image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}
// info dictionary keys
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaType; // an NSString (UTI, i.e. kUTTypeImage)
UIKIT_EXTERN NSString *const UIImagePickerControllerOriginalImage; // a UIImage
UIKIT_EXTERN NSString *const UIImagePickerControllerEditedImage; // a UIImage
UIKIT_EXTERN NSString *const UIImagePickerControllerCropRect; // an NSValue (CGRect)
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaURL; // an NSURL
UIKIT_EXTERN NSString *const UIImagePickerControllerReferenceURL NS_AVAILABLE_IOS(4_1); // an NSURL that references an asset in the AssetsLibrary framework
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaMetadata NS_AVAILABLE_IOS(4_1); // an NSDictionary containing metadata from a captured photo
// 获得编辑过的图片
UIImage* image = [info objectForKey: @"UIImagePickerControllerEditedImage"];
// 把图片转成NSData类型的数据来保存文件(存入到沙盒中)
NSData *imageData;
// 判断图片是不是png格式的文件
if (UIImagePNGRepresentation(image)) {
// 返回为png图像。
imageData = UIImagePNGRepresentation(image);
}else {
// 返回为JPEG图像
imageData = UIImageJPEGRepresentation(image, 1.0);
}
// 路径拼接,写入-----
NSString * imageSavePath = [[[HMTMySqliteDataHandle shareInstance]saveImagesPath] stringByAppendingPathComponent:@"自定义.自定义"];
[imageData writeToFile:imageSavePath atomically:YES];
// 关闭相册界面
[self dismissModalViewControllerAnimated:YES];
[picker release];
}
// 用户选择取消
- (void) imagePickerControllerDidCancel: (UIImagePickerController *)picker
{
// 关闭相册界面
[self dismissModalViewControllerAnimated:YES];
[picker release];
}
~~~
@[ UIImagePickerController详解](http://blog.csdn.net/kingsley_cxz/article/details/9157093)
@ [UIImagePickerController](http://blog.csdn.net/rhljiayou/article/details/8003939) 任海丽(3G移动/开发)
@[iOS摄像头和相册-UIImagePickerController-浅析](http://blog.sina.com.cn/s/blog_7b9d64af0101cfd9.html)