iphone - AFNetworking - 下载多个文件 + 通过 UIProgressView 监控

标签 iphone ios asihttprequest afnetworking

我正在尝试将我的代码从 ASIHTTPRequest 更改为 AFNetworking。目前我想选择 10-15 个不同的 HTTP URL(文件)并将它们下载到文档文件夹中。

使用 ASIHTTPRequest 非常简单

[myQueue setDownloadProgressDelegate:myUIProgressView];

在 AFNetworking 中,我不知道该怎么做。我有以下代码下载文件,存储它们并在文件下载成功时发出通知,但我无法为这个队列创建总大小的进度条。

for (i=0; i<3; i++) {

    NSString *urlpath = [NSString stringWithFormat:@"http://www.domain.com/file.zip"];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlpath]];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"testFile%i.zip",i]];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", path);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    [operation setDownloadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
        NSLog(@"Sent %d of %d bytes, %@", totalBytesWritten, totalBytesExpectedToWrite, path);
    }];

    [myQueue addOperation:operation];  
}

最佳答案

我认为您必须创建自己的 UIProgressView,我将在示例中将其称为 progressView。

progressVu = [[UIProgressView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[progressVu setProgressViewStyle: UIProgressViewStyleDefault];

然后只更新进度条:

[operation setDownloadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {

    float percentDone = ((float)((int)totalBytesWritten) / (float)((int)totalBytesExpectedToWrite));

    progressView.progress = percentDone;

    NSLog(@"Sent %d of %d bytes, %@", totalBytesWritten, totalBytesExpectedToWrite, path);
}];

关于iphone - AFNetworking - 下载多个文件 + 通过 UIProgressView 监控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9055779/

相关文章:

iphone - 某些 View Controller 独有的自动旋转

ios - 从 html 链接重定向到 IOS 应用程序

iphone - 如何在 ios 中使用 Graph API 识别消息已成功发布?

ios - UIView.animate 不适用于背景颜色

ios - 使用 SKShapeNode 绘制和删除线条

iphone - 关于使用 ASIHttpRequest 下载文件

由于 exc 访问错误,iphonesdk 值从数组中删除

objective-c - ASIHTTPRequest setTemporaryFileDownloadPath:不适用于某些URL

iphone - 在为 iphone 下载时弹出 View Controller 时来自 ASIHTTPRequest 的 exc_bad_access

ios - 无法在 UITableViewController 中设置 TableView 的约束