在对于图片上传操作时,我们需要先将图片展示给用户,然后点击按钮将图片上传。那么我们在点击上传按钮的时候,如果用户没有进行更改,我们就直接上传,要么是是空图片,要么是原始的默认图。而我们的目的是保证用户选择图片并上传,而不是上传默认图片。那么怎么区分图片是否已经选择好了呢? 对我而言,有两种方法:
1.申明全局变量保存图片 每当选择一张图片,就将图片赋值给全局的变量进行保存,但是如果需要展示和上传的图片过多时, 不仅需要创建大量的全局变量,而且在选择照片后还需要判定选择的是第几张
以展示和上传两张图片为例:
首先创建两个全局变量 image1, image2
@interface ASDUploadForMenHuFragment () <UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *cardFrontButton;
@property (weak, nonatomic) IBOutlet UIButton *co_Button;
@property (strong, nonatomic) UIImage *image1;
@property (strong, nonatomic) UIImage *image2;
@end
在选择照片后,对照片选择器的回调做处理
- (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(), ^{
if (selectedButton == weakSelf.cardFrontButton) {
image1 = image;
[weakSelf.cardFrontButton setBackgroundImage:image forState:UIControlStateNormal];
} else if (selectedButton == weakSelf.co_Button) {
image2 = image;
[weakSelf.co_Button setBackgroundImage:image forState:UIControlStateNormal];
}
});
}];
}
点击上传按钮的时候,我们进行判断的标准则为image1 与image2 是否为空
- (void)uploadImage {
if (image1 == nil) {
NSLog(@"image1未选择或更改");
return;
}
if (image2 == nil) {
NSLog(@"image2未选择或更改");
return;
}
//以下为选择照片并更改后的操作的代码
}
以上代码虽然解决了验证的问题,但是一旦展示和需要上传的照片过多,则需要创建很多的全局变量保存,代码会变得很冗长,且不好维护。
2.通过方法 setAccessibilityIdentifier ,设置标识符, 可减少代码量及方便代码阅读
同样以展示和上传两张照片为例:
@interface ASDUploadForMenHuFragment () <UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *cardFrontButton;
@property (weak, nonatomic) IBOutlet UIButton *co_Button;
@end
在viewdidLoad 方法中,我们设置展示按钮图片的默认标示
- (void)viewDidLoad {
[super viewDidLoad];
//设置图片默认标识符 此处标识符可以随意写
[self.cardFrontButton.currentBackgroundImage setAccessibilityIdentifier:@"S_cardFront"];
[self.co_Button.currentBackgroundImage setAccessibilityIdentifier:@"S_RenZheng"];
}
在选择完图片之后,在照片选择器的回调中,我们将默认的标识符进行改变
- (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(), ^{
if (selectedButton == weakSelf.cardFrontButton) {
[weakSelf.cardFrontButton setBackgroundImage:image forState:UIControlStateNormal];
//改变默认图片标识符
[weakSelf.cardFrontButton.currentBackgroundImage setAccessibilityIdentifier:@"imagePicked"];
} else if (selectedButton == weakSelf.co_Button) {
[weakSelf.co_Button setBackgroundImage:image forState:UIControlStateNormal];
//改变默认图片标识符
[weakSelf.co_Button.currentBackgroundImage setAccessibilityIdentifier:@"imagePicked"];
}
});
}];
}
在uploadImage方法中,我们就可以根据以上的设置,找出标识符进行判断
- (IBAction)uploadImageEvent:(UIButton *)sender {
//获取相应的标识符
NSString *imageName1 = [self.cardFrontButton.currentBackgroundImage accessibilityIdentifier];
NSString *imageName2 = [self.co_Button.currentBackgroundImage accessibilityIdentifier];
//将标识符进行判断
if ([imageName1 isEqualToString:@"S_cardFront"] && [imageName2 isEqualToString:@"S_RenZheng"]) {
ShowTips(@"请先选择要上传的照片");
return;
}
ShowIndicator_InView(self.view, @"正在上传照片");
[self imageUpload];
}
以上我们就可以很好的进行图片是否有过更改的判断, 不需要创建一大堆的全局变量进行保存, 在上传图片的时候,就可以直接取相应控件的图片值进行上传。看起来还是即简单也方便了很多。