AFNetworking + Cached Image Placeholder

688 Views Asked by At

In short, I'm looking for a way to use a previously cached image as my placeholder in AFNetworking's UIImageView Category:

- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
          placeholderImage:(UIImage *)placeholderImage 
                   success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
                   failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure

Example Use case:

I have a model named Post that I populate from an API response:

@interface Post : NSObject
    @property ... NSString *smallImagePath;
    @property ... NSString *largeImagePath;
    @property ... NSString *description;       
@end

My first ViewController will display a grid of thumbnails (retrieved via the smallImagePath property) for all my Post objects. Tapping on a thumbnail will take you to a DetailViewController, where the large image will ultimately display with the description.

Since I've already downloaded and cached the small thumbnail, I'd like to use it as the placeholder while I wait for the large image to be downloaded. At this point I can't figure out how to get into the AFImageCache since it's not publicly accessible.

One thing I thought of is storing the UIImage that is passed as an argument in the success block:

// add property to Post model
@property ... UIImage *smallImage;

success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
    [self.post setSmallImage:image];
}

And then I can use that image directly as the placeholder while I download the large image.

I'm not sure if this will have any noticeable/negative impact to my memory footprint by keeping references to a bunch of UIImage's though. Can you think of a better way?

1

There are 1 best solutions below

0
On BEST ANSWER

Ended up going with my original idea, which was to store the image as a property on my model. That way, when needing to declare a placeholder while the larger image loads, I just access that property. Performance has been great.