AFNetworking setImageWithURLRequest下载进度
问题描述:
我正在使用此代码将图像设置为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)]];
}];
答
开箱即用,没有的UIImageView + AFNetworking类别不具有此功能。但是,它可以很容易地通过增加这种方法的类别添加到:
-(void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block{
[self.af_imageRequestOperation setDownloadProgressBlock:block];
}
我已经回答了这个问题在[http://*.com/questions/34744024/afnetworking-3-0-setimagewithurlrequest-download-progress/36983966#36983966](http://*.com/questions/34744024/afnetworking-3-0-setimagewithurlrequest-download-progress/36983966#36983966) – Jegan