内存与AFNetworking

问题描述:

获取的图像时,我有我的iOS应用获取一张图像的时间越来越多,并显示在屏幕(非常像幻灯片)上。我遇到低内存的情况。在运行Allocations时查看仪器,我发现内存在不断增长。调用树显示正在使用大量的内存,但没有一行是我的代码,我不是在调试这些情况下,知道如何读它足够的经验。内存与AFNetworking

Call Tree from Instruments

下面是我与获取图像的代码:

- (void)fetchLargeImage:(void (^)(UIImage *))completion 
{ 

    // image doesn't exist yet, fetch it now 
    NSURL *resourceURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://path_to_image/%@", @"image_name.jpg"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:resourceURL]; 

    AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:^UIImage *(UIImage *image) { 
     return image; 
    } success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 

     completion(image); 

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
     // image wasn't loaded 
    }]; 

    [operation start]; 

} 

由于图像显示,我保存最后10幅图像,因为用户可以刷卡向后回顾前面所示图片。在我的清理方法中,我NIL出在图像视图和ImageView的自身的图像,并且从该阵列中删除。所以我不确定还有什么能够保持它,并导致内存继续增长。我在这里错过了什么?

AFNetworking的类别的UIImageView方法静静存储它们在共享AFImageCache对象,这是NSCache的一个子类中检索图像。这在性能方面非常出色,但我想它可能会导致您的内存问题。你会看到这条线是每个成功的AFImageRequestOperation后叫:好像

[[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:urlRequest]; 

类别方法不超过该提供多大的控制,据我可以告诉。如果你觉得舒服编辑在的UIImageView + AFNetworking.m的代码,你可以注释掉上面的行,或者这行添加到af_sharedImageCache类方法来限制高速缓存的大小:

[_af_imageCache setCountLimit: 10]; // limits the number of cached images to 10 

我没有使用仪器进行测试,所以我不能100%确定它会解决你的问题,但也许它有助于解释发生了什么。

+0

这听起来很有前途,但我没有看到任何效果运行的仪器。虽然谢谢! – Stephen 2013-02-12 05:23:21