相机选择照片及拍照后使用AFNetworking图片上传(支持一张及多张上传)


弹出照片获取方式
- (IBAction)changeTheImage:(UIButton *)sender {
    selectedButton = sender;
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"相机", nil];
    [sheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != actionSheet.cancelButtonIndex) {
        if (buttonIndex == 0) {
            //相册
            [self goAlbum];
        } else if (buttonIndex == 1) {
            //相机
            [self takePhoto];
        }
    }
}

//开始拍照
-(void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: sourceType]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = sourceType;
        picker.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
        picker.videoQuality = UIImagePickerControllerQualityTypeMedium;
        [self presentViewController:picker animated:YES completion:^{
            
        }];
    }
}

//相册选择
- (void)goAlbum {
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
        picker.videoQuality = UIImagePickerControllerQualityTypeMedium;
        [self presentViewController:picker animated:YES completion:^{
            
        }];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    //当选择的类型是图片
    //先把图片转成NSData
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //关闭相册界面
    __weak __typeof(self)weakSelf = self;
    [picker dismissViewControllerAnimated:YES completion:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setObject:image forKey:@"image"];
            if (selectedButton == weakSelf.cardFrontButton) {
                [dict setObject:@"card_img" forKey:@"type"];
                if (images.count < 1) {
                    [images addObject:dict];
                } else {
                    [images replaceObjectAtIndex:0 withObject:dict];
                }
                [weakSelf.cardFrontButton setBackgroundImage:image forState:UIControlStateNormal];
                [weakSelf.cardFrontButton.currentBackgroundImage setAccessibilityIdentifier:@"imagePicked"];
            } else if (selectedButton == weakSelf.co_Button) {
                
                [dict setObject:@"witness_one_img" forKey:@"type"];
                if (images.count < 2) {
                    [images addObject:dict];
                } else {
                    [images replaceObjectAtIndex:1 withObject:dict];
                }
                [weakSelf.co_Button setBackgroundImage:image forState:UIControlStateNormal];
                [weakSelf.co_Button.currentBackgroundImage setAccessibilityIdentifier:@"imagePicked"];
            }
        });
    }];
}

//上传照片 单张上传,将for循环去掉,获取到自己的需要上传的照片即可        多张上传的时候,利用for 循环,将照片一次性以form表单的形式提交到后台
- (void)imageUpload {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"loginName": self.scaningDict[@"username"],@"phone_num" : self.scaningDict[@"phoneNum"], @"session_id" :  self.scaningDict[@"sessionId"]};
    [manager POST:[NSString stringWithFormat:@"%@%@",SERVER_ADDRESS,URL_PHOTO_MENHU] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        for (int i = 0; i < images.count; i ++) {
            NSMutableDictionary *dict = images[i];
            NSData *data=UIImageJPEGRepresentation(dict[@"image"], .5);
            [formData appendPartWithFileData:data name:dict[@"type"] fileName:@"" mimeType:@""];
        }
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        id code=[responseObject objectForKey:@"code"];
        if ([code intValue] == 200) {
            dispatch_async(dispatch_get_main_queue(), ^{
                HideIndicator_InView(self.view);
                ShowTips(@"上传成功,请到门户查看");
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                });
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                HideIndicator_InView(self.view);
                ShowTips(@"上传失败,请重新上传");
                return ;
            });
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            HideIndicator_InView(self.view);
            ShowTips(@"上传失败,请重新上传");
            return ;
        });
    }];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值