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 に答える