在商城APP中,有些时候需要先处理图片上传。商品APP中,某个订单的立即评价,存在多个商品,一起评价。先将这多张图上传到服务器并返回图片对应的url,然后再把这些图片url和文字作为动态的属性发布到服务器。
IMG_3046.PNG
//创建信号量
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
//上传图片,网络请求的总数
NSInteger commandCount = [self.model.list count];
__block NSInteger httpFinishCount = 0;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (goodsOrderModel *goodsOrder in self.model.list) {
//是否有图片
if (goodsOrder.selectedPhotos.count>0) {
//上传图片,网络请求
[self UploadPhotoFile:goodsOrder.selectedPhotos block:^(BOOL isTrue) {
if (isTrue) {
NSLog(@"上传出错");
}else{
NSLog(@"上传成功");
++httpFinishCount;
}
if (httpFinishCount == commandCount) {
//发送一个信号
dispatch_semaphore_signal(sem);
}
}];
}else{
++httpFinishCount;
if (httpFinishCount == commandCount) {
//发送一个信号
dispatch_semaphore_signal(sem);
}
}
}
//等待信号
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"所有图片上传后,其他操作");
});
});
网友评论