Thursday, 23 May 2013

How to disable caching of a particular image in AFNetworking ios

Hi all

Recently i faced a problem while using AFNetworking's  UIImageView+AFNetworking category.
I was making a image editor but the issue was when i used to post the image to my file server after editing the URL send to me of the edited image was same as that of previously unedited image. and when i used this url with


- (void)setImageWithURL:(NSURL *)url    method i used to get the old unedited image which was there in cache.


so here is my solution.

1.take a global flag and set it to YES when you are using edited image url 

2. Then in  - (void)setImageWithURL:(NSURL *)url
       placeholderImage:(UIImage *)placeholderImage method of  UIImageView+AFNetworking

 - (void)setImageWithURL:(NSURL *)url
       placeholderImage:(UIImage *)placeholderImage
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPShouldHandleCookies:NO];
    [request setHTTPShouldUsePipelining:YES];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    if(cachingFlag)
    {
     request.cachePolicy=NSURLRequestReloadIgnoringCacheData;
    }
    [self setImageWithURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
}


add the red code you will thus not receive the cached image for the edited image.
 I have made a UIImageview category which explicitly provides this facility and works on the same principle as AFNetworking use and contribute as per your wish link
Happy coding

No comments:

Post a Comment