在 iOS 中使用 afnetworking 图片下载进度条不流畅
Image downloading progress bar not smooth using afnetworking in iOS
我目前正在使用afnetworking下载图片,但是第一次时进度条不流畅,但是当我运行第二次这段代码时,进度条很流畅,这是我的代码下载图片。
进度条像上升一样,下降时比较平滑,但是当我 运行 第二次编码时,它运行得很平滑
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];
}
此代码来自 2013 年的回答。我建议
不要使用已弃用的 AFHTTPRequestOperation
,而是使用 NSURLSession
下载 task-based 解决方案。如果您想使用 AFNetworking,他们有一种机制可以做到这一点。
不要自己 update/calculate 百分比,而是现在你会使用 NSProgress
来计算个人下载量,children 到 parent NSProgress
。你可以让你的 UIProgressView
观察到这一点。最终效果是您最终只更新了 child NSProgress
个实例,而 parent 的进度视图会自动更新。
例如,假设我有一个名为 totalProgressView
的 parent UIProgressView
并且我有一个正在观察的 NSProgress
:
@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
然后开始下载,我创建了一系列图像下载,将它们各自的 NSProgress
个实例添加为上述 children 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];
}
在哪里
// 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
和
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
这会为单独的下载生成单独的进度条,并且与 totalProgress
关联的进度条会自动为您更新,生成:
现在,很明显,您不需要同时使用 children UIProgressView
和 parent,所以这取决于您。但是思路是
- 设置层次
NSProgress
;
- 告诉
UIProgressView
观察任何你想要的 NSProgress
;和
- 只需让您的下载更新 child
NSProgress
值,其余的将自动为您完成。
我目前正在使用afnetworking下载图片,但是第一次时进度条不流畅,但是当我运行第二次这段代码时,进度条很流畅,这是我的代码下载图片。
进度条像上升一样,下降时比较平滑,但是当我 运行 第二次编码时,它运行得很平滑
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];
}
此代码来自 2013 年的回答。我建议
不要使用已弃用的
AFHTTPRequestOperation
,而是使用NSURLSession
下载 task-based 解决方案。如果您想使用 AFNetworking,他们有一种机制可以做到这一点。不要自己 update/calculate 百分比,而是现在你会使用
NSProgress
来计算个人下载量,children 到 parentNSProgress
。你可以让你的UIProgressView
观察到这一点。最终效果是您最终只更新了 childNSProgress
个实例,而 parent 的进度视图会自动更新。
例如,假设我有一个名为 totalProgressView
的 parent UIProgressView
并且我有一个正在观察的 NSProgress
:
@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
然后开始下载,我创建了一系列图像下载,将它们各自的 NSProgress
个实例添加为上述 children 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];
}
在哪里
// 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
和
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
这会为单独的下载生成单独的进度条,并且与 totalProgress
关联的进度条会自动为您更新,生成:
现在,很明显,您不需要同时使用 children UIProgressView
和 parent,所以这取决于您。但是思路是
- 设置层次
NSProgress
; - 告诉
UIProgressView
观察任何你想要的NSProgress
;和 - 只需让您的下载更新 child
NSProgress
值,其余的将自动为您完成。