2012-12-11 38 views
1

我有这种方法,它可以在单击单元格时下载UIImageView的全尺寸图像。AFNetworking检查图像是否缓存

- (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

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

+1

当我打开同一个单元格时,imageView已经加载了下载的图片,为什么? – 1337code

+0

当我关闭并打开应用程序并打开单元格时,图像未下载,它在那里 – 1337code