【问题标题】:AFNetworking 1.x to 3.x migration - get download progress?AFNetworking 1.x 到 3.x 迁移 - 获取下载进度?
【发布时间】:2016-02-12 08:56:31
【问题描述】:

我需要下载带有进度条的图像。这就是我在 AFNetworking 1.x 中所做的,但不适用于 AFNetworking 3.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];

【问题讨论】:

    标签: objective-c ios9 nsurlsession nsurlsessiondownloadtask afnetworking-3


    【解决方案1】:

    您可以获得最好的框架RSNetworkKit,它可以轻松处理所有网络调用。它在内部实现了AFNetworking 3.0。它会以单一方法为您提供下载进度,无论是上传还是下载。

    它还有一个增强的方法来显示 UIImageView 的进度。 你可以在这里找到它。 https://github.com/rushisangani/RSNetworkKit

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-22
      • 2016-04-04
      • 1970-01-01
      • 2023-03-29
      • 2018-08-10
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多