弹出照片获取方式
- (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 ;
});
}];
}
相机选择照片及拍照后使用AFNetworking图片上传(支持一张及多张上传)
最新推荐文章于 2020-12-24 08:35:59 发布