在商城APP中鱼响,有些時候需要先處理圖片上傳组底。商品APP中,某個訂單的立即評價江滨,存在多個商品厌均,一起評價棺弊。先將這多張圖上傳到服務(wù)器并返回圖片對應(yīng)的url,然后再把這些圖片url和文字作為動態(tài)的屬性發(fā)布到服務(wù)器稻艰。
//創(chuàng)建信號量
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
//上傳圖片侈净,網(wǎng)絡(luò)請求的總數(shù)
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) {
//上傳圖片,網(wǎng)絡(luò)請求
[self UploadPhotoFile:goodsOrder.selectedPhotos block:^(BOOL isTrue) {
if (isTrue) {
NSLog(@"上傳出錯");
}else{
NSLog(@"上傳成功");
++httpFinishCount;
}
if (httpFinishCount == commandCount) {
//發(fā)送一個信號
dispatch_semaphore_signal(sem);
}
}];
}else{
++httpFinishCount;
if (httpFinishCount == commandCount) {
//發(fā)送一個信號
dispatch_semaphore_signal(sem);
}
}
}
//等待信號
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"所有圖片上傳后躯保,其他操作");
});
});