0

我刚从 ASIHTTP 转移到 AFNetworking 库。我还在使用 Olivier Poitrey 和 Peter Steinberg 的 SDURLCache 库。我想缓存我将在我的应用程序中使用的所有图像。对于这些,我尝试了这个:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        for(NSURLRequest *imageRequest in imageRequestArray){
            AFURLConnectionOperation *operation2 = [[[AFURLConnectionOperation alloc] initWithRequest:imageRequest] autorelease];
            [queue addOperation: operation2];
            [operation2 waitUntilFinished];
}

当我需要显示图像时,我对每张图像都这样做:

  NSURL *url = [NSURL URLWithString:[cat imagePath]];
        NSURLRequest *imageRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0];


        AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:imageRequest 
                                                                                  imageProcessingBlock:nil
                                                                                             cacheName:@"nscache"
                                                                                               success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
                                                                                                   //NSData *responseData = [request responseData];
                                                                                                   imageView.alpha = 0.0;
                                                                                                   [imageView setImage:image forState:UIControlStateNormal];
                                                                                                   [UIView beginAnimations:@"ToggleViews" context:nil];
                                                                                                   [UIView setAnimationDuration:1.0];                
                                                                                                   imageView.alpha = 1.0;
                                                                                                   [UIView commitAnimations];
                                                                                               }
                                                                                               failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){}
                                              ];
        [operation start];

一段时间后,应用程序发出内存警告,然后关闭。为此,我做了以下事情:

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSLog(@"Memory Warning...");
}

它清除了缓存(我没有什么),但过了一会儿,应用程序再次关闭。

我该怎么做?

4

1 回答 1

0

问题可能是因为 ARC 没有自动设置在队列或后台线程上。我遇到了同样的问题,并通过以下文章解决了它:

https://agilewarrior.wordpress.com/2012/04/09/memory-leaks-with-arc/

于 2012-08-16T10:09:34.447 回答