-2

インターネットから任意のファイルをダウンロードしてドキュメント フォルダーに保存するアプリケーションを 1 つ作成したいと考えています。Google とこのサイトで検索したところ、AFNetworking を使用する方が優れていることがわかりました。このフレームワークではprogessView、ダウンロード ステータスを表示できます。

私は AFNetworking を初めて使用します。AFNetworking でファイルをダウンロードし、progessViewステータスのダウンロードを表示する 1 つのアプリケーションを作成する方法をコードで教えてください。

4

2 に答える 2

11

UIProgressviewを使用する

ここでのコードの進行状況は、使用される UIProgressview です

filepath : ファイルが保存されるパス[ドキュメント ディレクトリ]

輸入

#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

プログレスバー付きのAFNetworkingイメージのダウンロードについては、これを試してください。

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

このプロジェクトには 2 つのフォルダーがありません。hear からダウンロードできます。

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

于 2013-05-06T12:51:08.143 に答える