0

プログレスバー付きの画像をダウンロードする必要があります。これは、AFNetworking 3.x では機能しない AFNetworking 1.x で得たものです。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.mediaItem.media_item_hd_url]];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFImageResponseSerializer serializer];

[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    UIImage *image = responseObject;
    _image = image;

    [self.delegate browserItem:self loaded:1.0 finished:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];

[op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    CGFloat progress = ((CGFloat)totalBytesRead)/((CGFloat)totalBytesExpectedToRead);
    [self.delegate browserItem:self loaded:progress finished:NO];
}];

[[NSOperationQueue mainQueue] addOperation:op];

最後に、これは私が思いついたものであり、完全ではありません。コードブロックで進行状況を使用する方法がわかりません。

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.mediaItem.media_item_hd_url]];

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];

    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

        NSLog(@"File downloaded to: %@", filePath);
//        UIImage *image = response;
//        _image = image;

        [self.delegate browserItem:self loaded:1.0 finished:YES];

    }];

    [downloadTask resume];
4

1 に答える 1

0

すべてのネットワーク呼び出しを簡単に処理する最高のフレームワークRSNetworkKitを入手できます。内部的にAFNetworking 3.0 を実装しています。 アップロード、ダウンロードのいずれかの方法でダウンロードの進行状況を確認できます。

UIImageViewで進行状況を表示する拡張メソッドもあります。ここで見つけることができます。https://github.com/rushisangani/RSNetworkKit

于 2016-02-12T11:07:12.493 に答える