2

我正在使用此代码将图像设置为 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)]];
    }];
4

2 回答 2

2

开箱即用,没有 UIImageView+AFNetworking 类别没有此功能。但是,可以通过将此方法添加到类别中轻松添加:

-(void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead,   long long totalBytesExpectedToRead))block{
   [self.af_imageRequestOperation setDownloadProgressBlock:block];
}
于 2012-12-11T16:23:51.497 回答
1

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

于 2013-09-10T04:44:52.047 回答