-2

我想创建一个从 Internet 下载任何文件并存储在文件夹中的应用程序。我在谷歌和这个网站上搜索,我知道使用 AFNetworking 更好。在这个框架中,我可以显示progessView显示下载状态。

我是 AFNetworking 的新手,我希望您指导我并用代码告诉我如何创建一个使用 AFNetworking 下载文件并显示progessView下载状态的应用程序。

4

2 回答 2

11

使用UIProgressview

这里的代码进度是使用的 UIProgressview

filepath : 保存文件的路径[Documents directory]

进口

#import <AFNetworking/AFNetworking.h>

并下载

progress.progress = 0.0;

currentURL=@"http://www.selab.isti.cnr.it/ws-mate/example.pdf";


NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]];
AFURLConnectionOperation *operation =   [[AFHTTPRequestOperation alloc] initWithRequest:request];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"MY_FILENAME_WITH_EXTENTION.pdf"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];

[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
    progress.progress = (float)totalBytesRead / totalBytesExpectedToRead;

}];

[operation setCompletionBlock:^{
    NSLog(@"downloadComplete!");

}];
[operation start];
于 2013-05-06T11:16:39.957 回答
0

试试这个 For AFNetworking Image download with Progress bar。

https://www.cocoacontrols.com/controls/nprimageview

此项目中缺少两个文件夹,您可以从 hee 下载它

https://github.com/nicnocquee/NPRImageView/tree/master/Externals

于 2013-05-06T12:51:08.143 回答