gpt4 book ai didi

iphone - AFNetworking setImageWithURL请求下载进度

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:12:39 25 4
gpt4 key购买 nike

我正在使用这段代码将图像设置为 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 类别不具备此功能。但是,可以通过将此方法添加到类别中来轻松添加它:

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

关于iphone - AFNetworking setImageWithURL请求下载进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13821428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com