美文网首页iOS项目iOS Developer
iOS 使用ELCImagePicker实现相册照片多选和真机拍

iOS 使用ELCImagePicker实现相册照片多选和真机拍

作者: happyte | 来源:发表于2017-01-08 15:11 被阅读910次

    ELCImagePicker相册图片多选效果

    • 1.ELCImagePicker是第三方框架用来处理相册中的图片多选,在头文件中先导入#import "ELCImagePickerController.h"。实现的代码如下
     - (void)showImagePicker{
        ELCImagePickerController *imagePicker = [[ELCImagePickerController alloc] initImagePicker];
        imagePicker.maximumImagesCount = 3 - self.model.bookImageArray.count;
        imagePicker.returnsOriginalImage = YES;
        imagePicker.returnsImage = YES;
        imagePicker.onOrder = YES;
        imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
        imagePicker.imagePickerDelegate = self;
        
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
    

    第一个属性设置了相册允许选择照片的最大数量,这里最多选择3张,再减去现有的图片。第二个属性允许返回原始的图片,并且按照顺序返回。重要的是要设置控制器的代理。

    • 2.代理函数如下:
     #pragma mark - imagePickerController 代理
     - (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker {
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    
     - (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info {
        [picker dismissViewControllerAnimated:YES completion:nil];
        
        for (NSDictionary *dict in info) {
            if ([dict objectForKey:UIImagePickerControllerOriginalImage]) {
                UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
                // 压缩图片
                UIImage *resizedImage = [image compressImage:300];
                [self.model.bookImageArray addObject:resizedImage];
            }
        }
        [self.imageCollectionView reloadData];
    }
    

    在代理函数中返回原始图片,在UIImage分类中写了一个函数-- (UIImage *)compressImage:(CGFloat)size 函数用来压缩图片。

    真机拍照处理

    • 1.由于在模拟器无法使用相机,弹出警告框,需要真机模拟,使用UIImagePickerController 控制器,代码如下:
     - (void)showCamera{
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //    picker.allowsEditing = YES;
        // 监测权限
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:nil];
        } else if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
            UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您设置允许APP访问您的相机\n设置>隐私>相机" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alart show];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"设备不支持相机!" message:@"请从相册中选取照片。" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
        }
    }
    
    • 2.代理处理函数如下:
    #pragma mark - UIImagePickerController代理
     - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    
     - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
        [self dismissViewControllerAnimated:YES completion:nil];
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        UIImage *resizedImage = [image compressImage:300];
        [self.model.bookImageArray addObject:resizedImage];
        [self.imageCollectionView reloadData];
    }
    

    相关文章

      网友评论

      本文标题:iOS 使用ELCImagePicker实现相册照片多选和真机拍

      本文链接:https://www.haomeiwen.com/subject/roiqbttx.html