如何封装选择相册
1.很多时候,我们都在苦恼,该如何选择相册的图片;
首先,/*** 相册选择*/ @interface SXPickPhoto :NSObject
一个工具类,在这里,我将选择照片的调用系统的一系列方法进行了简单实现;
/**
* 打开相机:
*
* @param object 控制器对象
*/
- (void)ShowTakePhotoWithController: (UIViewController *)Controller
andWithBlock: (myblock)block;
/**
* 选择相册
*
* @param Controller 控制器对象
*/
- (void)SHowLocalPhotoWithController: (UIViewController *)Controller
andWithBlock: (myblock)block;
2.其中
andWithBlock: (myblock)block
是我用于回调图片UIimage * image
对象的,为了是代码更加清晰,我将相册和相机两个方法分开了。
下面直接上代码:
/**
* 打开相机:
*
* @param object 控制器对象
*/
- (void)ShowTakePhotoWithController: (UIViewController *)Controller
andWithBlock: (myblock)block
{
//回调
_myblock = block;
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
_picker.delegate = self;
//设置拍照后的图片可被编辑
_picker.allowsEditing = YES;
_picker.sourceType = sourceType;
[Controller presentViewController:_picker animated:YES completion:nil];
}
else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
}
3.具体解释就不再赘述:
/**
* 选择相册
*
* @param Controller 控制器对象
*/
- (void)SHowLocalPhotoWithController: (UIViewController *)Controller
andWithBlock: (myblock)block
{
_myblock = block;
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_picker.delegate = self;
//设置选择后的图片可被编辑
_picker.allowsEditing = YES;
[Controller presentViewController:_picker animated:YES completion:nil];
}
4.因为,前面有设置代理,图片在选择完以后,会调用以下代理方法:
//当一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
if (_myblock) {
_myblock(image);
[_picker dismissViewControllerAnimated:YES completion:nil];
}
}
}
// 取消选择照片:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"您取消了选择图片");
[picker dismissViewControllerAnimated:YES completion:nil];
}
5.最后是如何使用选择相册:
#pragma mark - === 提示框 ===
- (void)aleShowView
{
UIAlertAction * act1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
//拍照:
UIAlertAction * act2 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//打开相机
[_pickPhoto ShowTakePhotoWithController:self andWithBlock:^(NSObject *Data) {
if ([Data isKindOfClass:[UIImage class]])
{
_imageView.image = (UIImage *)Data;
}
}];
}];
//相册
UIAlertAction * act3 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//打开相册
[_pickPhoto SHowLocalPhotoWithController:self andWithBlock:^(NSObject *Data) {
if ([Data isKindOfClass:[UIImage class]])
{
_imageView.image = (UIImage *)Data;
}
}];
}];
UIAlertController * aleVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"选择图片" preferredStyle:UIAlertControllerStyleActionSheet];
[aleVC addAction:act1];
[aleVC addAction:act2];
[aleVC addAction:act3];
[self presentViewController:aleVC animated:YES completion:nil];
}