AFNetworking setImageWithURLRequest下载进度

我正在使用此代码将图像设置为UIImageView。

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 }]; 

但是我想用这个方法来跟踪下载进度,是否可以在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类没有这个function。 但是,通过将此方法添加到类别中,可以很容易地添加它:

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

看看这个cocoa豆: https : //github.com/xmartlabs/XLRemoteImageView 。 它使用objective-c内部实现你想要的。 我希望它可以帮助你。