美文网首页
图片多选的实现

图片多选的实现

作者: codeing小牛 | 来源:发表于2016-09-23 16:16 被阅读79次

    图片多选的实现 分为两部分
    1 调用相机拍照添加图片
    2 打开相册添加图片

    1 使用相机添加图片

    -(void)addimagePicker{
    // 遵守UIImagePickerControllerDelegate 协议创建imagePicker对象
        UIImagePickerController *pickercontroller = [[UIImagePickerController alloc]init];
        pickercontroller.allowsEditing = YES ;
        pickercontroller.delegate = self ;
        _pickercontroller = pickercontroller ;
    }
    
    调用相机时进行如下设置
    _pickercontroller.sourceType = UIImagePickerControllerSourceTypeCamera ;
                // 默认是打开相机 也可以设置为录制视频
                _pickercontroller.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto ;
                [self presentViewController:_pickercontroller animated:YES completion:nil];
    
    实现imagePicker 的代理方法(拍照完成获取image)
    // 用户选中某条媒体信息之后的处理
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
        // 获取媒体类型(相机 or 相册 or 视频 )
        NSString *mediatype = [info objectForKey:UIImagePickerControllerMediaType];
        if ([mediatype isEqualToString:@"public.image"]) {
            UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
            // 将拍好的图片存入系统相册
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
            // 对图片数据进行压缩
            NSData *data = UIImageJPEGRepresentation(image, 0.2);     
            UIImage *resultimage = [[UIImage alloc]initWithData:data];
    }
    

    打开相册添加图片
    使用第三方QBImagePicker 实现
    1 遵守协议
    QBImagePickerControllerDelegate
    2 创建图片选择器对象

    -(void)addQBImagePicker{
        QBImagePickerController *imagePickerController = [QBImagePickerController new];
        imagePickerController.delegate = self;
        imagePickerController.mediaType = QBImagePickerMediaTypeAny;
        imagePickerController.allowsMultipleSelection = 20;
        imagePickerController.showsNumberOfSelectedAssets = YES;
        _qbimagepicker = imagePickerController ;
    }
    

    3 实现代理方法

    - (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets
    {
        PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
        options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    
        for (PHAsset *asset in assets) {
            
            [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:[UIScreen mainScreen].bounds.size contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
                NSDictionary *dic = @{@"image":result,@"isselect":@"0"};
                [_selectArr addObject:dic];
                if (_selectArr.count == assets.count) {
                    // 先确定范围 range
                    NSRange range = NSMakeRange(_dataArr.count-1,_selectArr.count);
                    // 创建一个indexset对象
                    NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
                    // 插入数组中的元素
                [_dataArr insertObjects:_selectArr atIndexes:set];
                [_selectArr removeAllObjects];
                [_collectionView reloadData];
                }
            }];
        }
    
        [self dismissViewControllerAnimated:YES completion:NULL];
    }
    
    - (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController
    {
        [self dismissViewControllerAnimated:YES completion:NULL];
    }
    
    

    相关文章

      网友评论

          本文标题:图片多选的实现

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