I use this code to upload images.
-(IBAction) downloadButton:(id)sender { if(_downloadTask == nil){ _url1 =[NSURL URLWithString:@"someUrl"]; _url2 =[NSURL URLWithString:@"someUrl"]; _downloadTask = [_session downloadTaskWithURL:_url1]; _downloadTask1 = [_session downloadTaskWithURL:_url2]; [_downloadTask resume]; [_downloadTask1 resume]; } else [_downloadTask resume]; [_downloadView removeFromSuperview]; [_downloadButton removeFromSuperview]; [_downloadButtonCancel removeFromSuperview]; } } - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { _paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); _documentsDirectory1 = [_paths1 objectAtIndex:0]; _filePath1 = [_documentsDirectory1 stringByAppendingPathComponent:@"1.png"]; _fileExists1 = [[NSFileManager defaultManager] fileExistsAtPath:_filePath1 isDirectory:false]; _paths2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); _documentsDirectory2 = [_paths2 objectAtIndex:0]; _filePath2 = [_documentsDirectory2 stringByAppendingPathComponent:@"2.png"]; _fileExists2 = [[NSFileManager defaultManager] fileExistsAtPath:_filePath2 isDirectory:false]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *urlData1 = [NSData dataWithContentsOfURL:_url1]; [urlData1 writeToFile:_filePath1 atomically:YES]; NSData *urlData2 = [NSData dataWithContentsOfURL:_url2]; [urlData2 writeToFile:_filePath2 atomically:YES]; }); And this code is to show the progress of loading in percent.
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite; { dispatch_async(dispatch_get_main_queue(), ^{ float progress = [[NSNumber numberWithInteger:totalBytesWritten] floatValue]; float total = [[NSNumber numberWithInteger:totalBytesExpectedToWrite] floatValue]; NSString *percentage = [NSString stringWithFormat:@"%.f%%", ((progress / total) * 100)]; NSLog(@"%.f%%", percentage); if (!_label) { _label = [[UILabel alloc] initWithFrame:CGRectMake(300, 130, 42, 19)]; _label.numberOfLines = 1; _label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; _label.adjustsFontSizeToFitWidth = YES; _label.minimumScaleFactor = 10.0f/12.0f; _label.clipsToBounds = YES; _label.backgroundColor = [UIColor clearColor]; _label.textColor = [UIColor whiteColor]; _label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:_label]; } _label.text = percentage; The problem is that the label does not display a load from 1% to 100%. and randomly shows different numbers. How to fix it?