AFNetworking/AFNetworking — pull request #975 by aufflick (2013-05-07 13:50:57+00:00)
Title: Changes to provide controlled access to UIImage+AFNetworking's cache

While I agree with hiding the cache implementation in general, there are some circumstances where exposing minimal access is required:

1. A way to clear cache entries when they are known to be stale;
2. A way to triage which images are stored in the cache.

An example of (1) is after uploading a new user profile image which is delivered by a fixed endpoint. For this change I opted for a simple clearing of the cache (unlike NSURLCache where you can clear individual entries with `storeCachedResponse:forRequest:`).

Two example of (2) are:

a. restricting the size of images you want to cache;
b. preventing partial/corrupted images from being placed in the cache.

For me, (b) was the driver of the change since the lovely AT&T network was truncating some downloads and NSURLConnection was treating it as a successful download. Here is how I use the api added in this change to prevent that in my case:

```objective-c
[UIImageView setAFNetworkingShouldAcceptImageBlock:^AFNetworkingShouldAcceptImage(NSData *originalData, NSURLRequest *originalRequest, NSError *__autoreleasing *error) {
if ([[originalRequest.URL pathExtension] isEqualToString:@"jpg"])
return dataIsValidJPEG(originalData) ? AFNetworkingShouldAcceptImageYES : AFNetworkingShouldAcceptImageNO;

return AFNetworkingShouldAcceptImageYES;
}];
```

I have included both changes in a single pull request since they are both related to exposing a limited API for the private cache.