IOS中AFNetworking Cache映像中的内存泄漏

时间:2012-04-10 11:21:54

标签: ios memory-leaks afnetworking

我刚刚从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...");
}

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

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

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