0

I am using this block for a synchronous image downloading in an iOS 5 app. The logic is simple, if image is in cache use it, if not download it, I suppose NSURLRequestReturnCacheDataElseLoad has exactly this behavior.

    [factory.downloadImagesQueue addOperationWithBlock:^(){
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",IP2BaseImgURL,imgName]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url
                         cachePolicy:NSURLRequestReturnCacheDataElseLoad
                     timeoutInterval:15.0];

        NSCachedURLResponse* cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
        if (cachedResponse!=nil) {
            NSLog(@"%@ found in cache",imgName);
            [delegate imageDidFinishDownload:[UIImage imageWithData:[cachedResponse data]] atIndexPath:indexPath];
        } else {
            NSError *error;
            NSURLResponse *response;
            NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
            if (data == nil) {
                FULL_ERROR_LOG(error)
            } else {
                NSLog(@"%@ downloaded",imgName);
                if (![response.MIMEType isEqualToString:@"image/png"] && ![response.MIMEType isEqualToString:@"image/jpeg"]) {
                    NSLog(@"Found wrong mime type: %@",response.MIMEType);
                    [delegate imageDidEncounterErrorWhileDownload:imgName atIndexPath:indexPath];
                } else {
                    NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data];
                    [[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:(NSURLRequest *)request];
                    [delegate imageDidFinishDownload:[UIImage imageWithData:data] atIndexPath:indexPath];
                }
            }
        }

    }];

I did a test with three images. When I am downloading them for first time, I can see the message 'downloaded MyimgName1.jpg' 'downloaded MyimgName2.jpg' 'downloaded MyimgName3.jpg', and by opening the Cache.db I can see all my images correctly stored in it.

However when I am trying to download again, only the very first downloaded images show the message 'MyimgName1.jpg found in cache', which means that the cachedResponse is nil for all the other 2 and 3 request:

    NSCachedURLResponse* cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
    if (cachedResponse!=nil) { // <-- nil
    }

am I missing something in the settings or creation of NSCache ?

4

1 に答える 1

0

この線:

[[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:(NSURLRequest *)request];

「cachedResponse」の値が nil の場合は、if の内部にあるため、常に nil を格納します。どうやって何かをキャッシュしているのだろうか。

とにかく、キャッシュ内に挿入する前に、NSCachedURLResponse の新しいインスタンスを割り当てる必要があります。補足として、キャッシュの容量を変更してみてください。これが問題になる可能性があります。setMemoryCapacity で変更できます。

于 2012-11-10T11:47:11.873 に答える