AFNetworking检查图像是否被缓存

时间:2012-12-11 16:23:52

标签: iphone ios ipad caching afnetworking

我有这种方法,当点击单元格时会下载UIImageView的完整尺寸图像。

- (void)configureView
{
    NSURLRequest *URLRequest = [[NSURLRequest alloc] initWithURL:self.imageURL];

    AFImageRequestOperation *operation = [[AFImageRequestOperation alloc] initWithRequest:URLRequest];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            [SVProgressHUD showSuccessWithStatus:@"Done!"];

        [self.imageView setImage:responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        float percentage = (totalBytesRead / (totalBytesExpectedToRead * 1.0f) * 100);
        NSLog(@"Percentage: %f", percentage);
        if (percentage != 100.0)
        {
            [SVProgressHUD showWithStatus:[NSString stringWithFormat:@"Downloading... %0.0f%%", percentage]];
        }
    }];

    [operation start];
}

因此,下载完成后,将调用[SVProgressHUD showSuccessWithStatus:@"Done!"];。 但是当您返回tableViewController并单击同一个单元格时,即使图像被缓存,也会再次调用[SVProgressHUD showSuccessWithStatus:@"Done!"];。 如何检查图像是否已缓存,如果不是,则仅调用[SVProgressHUD showSuccessWithStatus:@"Done!"];

1 个答案:

答案 0 :(得分:1)

AFImageRequestOperation不会缓存图像。我认为你将AFNetworking的类别“UIImageView + AFNetworking”与“AFImageRequestOperation”混为一谈。该类别确实缓存图像,AFImageRequestOperation没有。