我们经常会遇到上传图片给后台,然后后台审核不通过,需要重新删除上传等操作,如图:
思路是用三个新数组
- 1、userImagesList用户选择图片
- 2、oldImagesList记录后台的url图片
- 3、totalM本地和后台全部的图片
关键代码详解
#pragma mark - LAAttacheVDelegate
- (void)attacheV:(LAAttacheV *)attacheV didClickImageV:(LAImageModel *)imgModel {
if (imgModel.add) {
NSInteger count = self.model.userImagesList.count + self.model.oldImagesList.count;
[self choosePicture:9-count];
return;
}
// 预览
NSInteger index = imgModel.index;
if (index >= self.model.userImagesList.count) { // 证明是后台数据
index -= self.model.userImagesList.count;
NSLog(@"点击预览的是后台数据 = %ld",index);
} else { // 证明是本地数据
NSLog(@"点击预览的是本地数据 = %ld",index);
}
}
- (void)attacheV:(LAAttacheV *)attacheV didClickCloseV:(LAImageModel *)imgModel {
NSInteger index = imgModel.index;
if (index >= self.model.userImagesList.count) { // 证明是后台数据
index -= self.model.userImagesList.count;
NSLog(@"删除的是后台数据 = %ld",index);
[self.model.oldImagesList removeObjectAtIndex:index];
} else { // 证明是本地数据
NSLog(@"删除的是本地数据 = %ld",index);
[self.model.userImagesList removeObjectAtIndex:index];
}
[self refreshUI];
}
- 1、点击上传图片,
count
是由用户选择的图片个数加上后台返回的可操作的图片个数 - 2、预览图片,这里
index
下标大于userImagesList
用户选择的图片个数证明是后台数据,因为在布局UI的时候先添加了本地图片,后添加后台图片 - 3、删除图片同预览图片一样逻辑,删除对应
userImagesList
或者oldImagesList
即可,这里就是用一个新的数组oldImagesList
代替后台返回的imagesList
数组,保证数据的准确
#pragma mark - TZImagePickerControllerDelegate
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
NSMutableDictionary *imgDicM = [NSMutableDictionary dictionary];
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
[self showMessage:@"导入图片中..." toView:nil];
for (NSInteger i = 0; i < assets.count; i++) {
PHAsset *asset = assets[i];
dispatch_group_enter(group);
dispatch_async(queue, ^{
[[TZImageManager manager] getPhotoWithAsset:asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
if (photo) {
if (!isDegraded) { // 高清的
dispatch_group_leave(group);
LAImageModel *model = [[LAImageModel alloc] init];
model.image = photo;
[imgDicM setObject:model forKey:@(i).stringValue];
}
}
}];
});
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 等前面的异步操作都执行完毕后,回到主线程.
[self hideHUDForView:nil];
NSMutableArray *imgIndexM = [NSMutableArray array];
[imgDicM enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL * _Nonnull stop) {
[imgIndexM addObject:@(key.integerValue)];
}];
// 排序
NSArray *imgSortResults = [imgIndexM sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}];
// 重新整合有序的数据
NSMutableArray *tempM = [NSMutableArray array];
for (NSNumber *resultIndex in imgSortResults) {
LAImageModel *model = [imgDicM valueForKey:[resultIndex stringValue]];
[tempM addObject:model.image];
}
// 存到用户选择的图片数组中
for (id photo in self.model.userImagesList) {
[tempM addObject:photo];
}
self.model.userImagesList = tempM;
[self refreshUI];
});
}
- 1、因为选择图片使用的框架是
TZImagePickerController
,在获取原图的时候是异步的,这里使用了线程组 - 2、当所有异步操作完成后,先进行排序处理,最后再添加到
userImagesList
用户选择的数组里
#pragma mark - Action
- (IBAction)submitAction:(id)sender {
NSInteger userCount = self.model.userImagesList.count; // 用户手选本地的图片
NSInteger oldCount = self.model.oldImagesList.count; // 后台已有的网络图片
NSInteger totalCount = userCount + oldCount;
if (totalCount == 0) {
NSLog(@"请上传图片");
return;
}
if (userCount != 0) { // 用户选了本地图片,先上传给阿里云转http图片,在拼接给后台
[self showMessage:@"上传中..." toView:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self hideHUDForView:nil];
// 制造假数据
NSMutableArray *tempM = [NSMutableArray array];
for (NSInteger i = 0; i < userCount; i++) {
// 用户选多少张,就取多少张url图片
[tempM addObject:self.dataImgLists[i]];
}
// 再添加上之前后台返回的url图片数据
[tempM addObjectsFromArray:self.model.oldImagesList];
[self postHttpWithImageList:tempM];
});
} else { // 用户没有选择本地图片,直接用后台返回的图片url给后台
[self postHttpWithImageList:self.model.oldImagesList];
}
}
- 1、提交时,先判断
userImagesList
和oldImagesList
是否有值,因为这两个数组都是在操作的 - 2、假如用户选择了本地图片,则需要先上传到阿里云或者其他的平台,转为url后再和之前后台返回的url一起提交给后台
- 3、如果没有选择本地图片,就直接用
oldImagesList
旧的url数组数据提交给后台