0

我需要下载带进度条的图片。这是我在AFNetworking 1.x中得到的,它不适用于AFNetworking 3.x.AFNetworking 1.x到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]; 

回答

0

你可以得到最好的框架RSNetworkKit负责处理所有与放心的网络通话。它已在内部实施AFNetworking 3.0。它会为您提供单一方法下载进度,您可以上传,下载。

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