I am currently downloading the image using afnetworking, but the progress bar is not smooth in the first time, but when I run this code second time, the progress bar is smooth, here is my code to download images.
progress bar works like go up, down than smooth, but when I run code the second time it works smooth
progressBar.progress = 0.0;
self.imageDownloads=[[NSMutableArray alloc]init];
[self.imageDownloads addObject:[[ImageDownload alloc] initWithURL:[NSURL URLWithString:@""]];
for (int i=0; i < self.imageDownloads.count; i++)
{
ImageDownload *imageDownload = self.imageDownloads[i];
imageDownload.filename = [NSString stringWithFormat:@"MyImage%d",i];
[self downloadImageFromURL:imageDownload];
}
Here is my code to download images
- (void)downloadImageFromURL:(ImageDownload *)imageDownload
{
NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [docsPath stringByAppendingPathComponent:imageDownload.filename];
NSURLRequest *request = [NSURLRequest requestWithURL:imageDownload.url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
imageDownload.totalBytesRead = totalBytesRead;
imageDownload.totalBytesExpected = totalBytesExpectedToRead;
[self updateProgressView];
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSAssert([responseObject isKindOfClass:[NSData class]], @"expected NSData");
NSData *responseData = responseObject;
[responseData writeToFile:filePath atomically:YES];
// Because totalBytesExpected is not entirely reliable during the download,
// now that we're done, let's retroactively say that total bytes expected
// was the same as what we received.
imageDownload.totalBytesExpected = imageDownload.totalBytesRead;
[self updateProgressView];
NSLog(@"finished %@", imageDownload.filename);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error %@", imageDownload.filename);
}];
[operation start];
}
- (void)updateProgressView
{
double totalTotalBytesRead = 0;
double totalTotalBytesExpected = 0;
for (ImageDownload *imageDownload in self.imageDownloads)
{
// note,
// (a) totalBytesExpected is not always reliable;
// (b) sometimes it's not present at all, and is negative
//
// So, when estimating % complete, we'll have to fudge
// it a little if we don't have total bytes expected
if (imageDownload.totalBytesExpected >= 0)
{
totalTotalBytesRead += imageDownload.totalBytesRead;
totalTotalBytesExpected += imageDownload.totalBytesExpected;
}
else
{
totalTotalBytesRead += imageDownload.totalBytesRead;
totalTotalBytesExpected += (imageDownload.totalBytesRead > kDefaultImageSize ? imageDownload.totalBytesRead + kDefaultImageSize : kDefaultImageSize);
}
}
if (totalTotalBytesExpected > 0)
[progressBar setProgress:totalTotalBytesRead / totalTotalBytesExpected animated:YES];
else
[progressBar setProgress:0.0 animated:NO];
}
This code is from an answer back in 2013. I would suggest
Don’t use the deprecated AFHTTPRequestOperation
, instead use NSURLSession
download task-based solution. If you want to use AFNetworking, they have a mechanism to do that.
Don’t update/calculate percentages yourself, but rather nowadays you’d use NSProgress
for the individual downloads which are children to some parent NSProgress
. You can have your UIProgressView
observe that. The net effect is that you end up just updating the child NSProgress
instances, and your parent’s progress view is updated automatically.
For example, imagine that I have a parent UIProgressView
called totalProgressView
and I have a NSProgress
that it is observing:
@interface ViewController () <UITableViewDataSource>
@property (nonatomic, strong) NSProgress *totalProgress;
@property (nonatomic, strong) NSMutableArray <ImageDownload *> *imageDownloads;
@property (nonatomic, weak) IBOutlet UIProgressView *totalProgressView;
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.totalProgress = [[NSProgress alloc] init];
self.totalProgressView.observedProgress = self.totalProgress;
self.tableView.estimatedRowHeight = 50;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.imageDownloads = [NSMutableArray array];
}
...
@end
Then to start the downloads, I create a series of image downloads, add their individual NSProgress
instances as children of the above totalProgress
:
- (IBAction)didTapStartDownloadsButton {
NSArray <NSString *> *urlStrings = ...
NSURL *caches = [[[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:true error:nil] URLByAppendingPathComponent:@"images"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
self.totalProgress.totalUnitCount = urlStrings.count;
for (NSInteger i = 0; i < urlStrings.count; i++) {
NSURL *url = [NSURL URLWithString:urlStrings[i]];
NSString *filename = [NSString stringWithFormat:@"image%ld.%@", (long)i, url.pathExtension];
ImageDownload *imageDownload = [[ImageDownload alloc] initWithURL:url filename:filename];
[self.imageDownloads addObject:imageDownload];
[self.totalProgress addChild:imageDownload.progress withPendingUnitCount:1];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
[imageDownload updateProgressForTotalBytesWritten:downloadProgress.completedUnitCount
totalBytesExpectedToWrite:downloadProgress.totalUnitCount];
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
return [caches URLByAppendingPathComponent:filename];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
//do whatever you want here
}];
[task resume];
}
[self.tableView reloadData];
}
Where
// ImageDownload.h
@import Foundation;
NS_ASSUME_NONNULL_BEGIN
@interface ImageDownload : NSObject
@property (nonatomic, strong) NSURL *url;
@property (nonatomic, strong) NSString *filename;
@property (nonatomic) NSProgress *progress;
@property (nonatomic) NSUInteger taskIdentifier;
- (id)initWithURL:(NSURL *)url
filename:(NSString * _Nullable)filename;
/**
Update NSProgress.
@param totalBytesWritten Total number of bytes received thus far.
@param totalBytesExpectedToWrite Total number of bytes expected (may be -1 if unknown).
*/
- (void)updateProgressForTotalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;
@end
NS_ASSUME_NONNULL_END
and
static const long long kDefaultImageSize = 1000000; // what should we assume for totalBytesExpected if server doesn't provide it
@implementation ImageDownload
- (id)initWithURL:(NSURL *)url filename:(NSString *)filename {
self = [super init];
if (self) {
_url = url;
_progress = [NSProgress progressWithTotalUnitCount:kDefaultImageSize];
_filename = filename ?: url.lastPathComponent;
}
return self;
}
- (void)updateProgressForTotalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
int64_t totalUnitCount = totalBytesExpectedToWrite;
if (totalBytesExpectedToWrite < totalBytesWritten) {
if (totalBytesWritten <= 0) {
totalUnitCount = kDefaultImageSize;
} else {
double written = (double)totalBytesWritten;
double percent = tanh(written / (double)kDefaultImageSize);
totalUnitCount = written / percent;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
self.progress.totalUnitCount = totalUnitCount;
self.progress.completedUnitCount = totalBytesWritten;
});
}
@end
That yields individual progress bars for the individual downloads, and the progress bar associated with the totalProgress
is updated automatically for you, yielding:
Now, obviously, you don't need both the children UIProgressView
and the parent one, too, so that's up to you. But the idea is
NSProgress
;UIProgressView
to observe whatever NSProgress
you want; andNSProgress
values and the rest will happen automatically for you.