1

I'm using UIImageView+AFNetworking to load images from a remote server on a table view. However, in some situations, the wrong images are cached (on NSURLCache, not only AFImageCache), i.e. the cache from a URL returns a response from another one (both used on the app).

My tableView:cellForRowAtIndexPath: is pretty normal:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.sections[indexPath.section][indexPath.row];

    static NSString *CellIdentifier = @"Cell";
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UIImage *placeholder = [[UIImage imageNamed:@"shield-flat"] tintedImageWithColor:[UIColor asbestosColor]];
    if (dict[@"first_url"] != [NSNull null]) {

        NSURLRequest *mandanteReq = [NSMutableURLRequest imageRequestWithURL:[NSURL URLWithString:dict[@"first_url"]]];
        [cell.firstImageView setImageWithURLRequest:mandanteReq placeholderImage:placeholder success:NULL failure:NULL];
    } else {
        cell.firstImageView.image = placeholder;
    }


    if (dict[@"second_url"] != [NSNull null]) {
        NSURLRequest *visitanteReq = [NSMutableURLRequest imageRequestWithURL:[NSURL URLWithString:dict[@"second_url"]]];
        [cell.secondImageView setImageWithURLRequest:visitanteReq placeholderImage:placeholder success:NULL failure:NULL];
    } else {
        cell.secondImageView.image = placeholder;
    }
    return cell;

}

Also, my category on NSMutableURLRequest that is used to create the request:

+ (NSMutableURLRequest *)imageRequestWithURL:(NSURL *)url {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    request.cachePolicy = NSURLRequestUseProtocolCachePolicy;
    request.HTTPShouldHandleCookies = NO;
    request.HTTPShouldUsePipelining = YES;
    request.timeoutInterval = 10;
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];

    return request;
}

I know that dict is the current object and that the URLs are fine too. I also trust that the server (Amazon S3) is sending the correct image.

The problem often occurs on the first install of the app (probably because the images are cached after).

Any thoughts?

4

0 回答 0