2012-12-11 63 views
2

我正在使用此代码将图像设置为UIImageView。AFNetworking setImageWithURLRequest下载进度

NSURLRequest *URLRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:imageToLoad]]; 

    [imageView setImageWithURLRequest:URLRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
     [cell.image setImage:image]; 
     [cell.activityIndicator stopAnimating]; 

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
     // Nothing 
    }]; 

但我想跟踪与方法下载进度,是possbile做到在setImageWithURLRequest方法?

通常我这样做是为了显示加载进度百分比:

[SVProgressHUD showWithStatus:@"Start download..."]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:link]]; 
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     // success 
     [SVProgressHUD showSuccessWithStatus:@"Done."]; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     // failed 
     [SVProgressHUD showErrorWithStatus:@"Failed."]; 
    }]; 

    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { 
     [SVProgressHUD showWithStatus:[NSString stringWithFormat:@"Downloading... %0.0f%%", totalBytesRead*100*1.0/(totalBytesRead+totalBytesExpectedToRead)]]; 
    }]; 
+0

我已经回答了这个问题在[http://stackoverflow.com/questions/34744024/afnetworking-3-0-setimagewithurlrequest-download-progress/36983966#36983966](http://stackoverflow.com/questions/34744024/afnetworking-3-0-setimagewithurlrequest-download-progress/36983966#36983966) – Jegan

回答

2

开箱即用,没有的UIImageView + AFNetworking类别不具有此功能。但是,它可以很容易地通过增加这种方法的类别添加到:

-(void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block{ 
    [self.af_imageRequestOperation setDownloadProgressBlock:block]; 
} 
+0

你能解释一下怎么做吗? – 1337code

+0

注意:截至2013年5月,看起来你*可以*实际上只是调用'setDownloadProgressBlock' - 不需要该类别 – taber