从服务器下载和保存大量图像时的iOS内存问题(ARC)

ohr*_*ohr 4 memory image download nsfilemanager ios

以下代码从不同大小的服务器下载700多个图像,这里的问题是内存(即使使用ARC)永远不会被释放,最终会出现内存警告,然后应用程序退出.我在这种方法中尝试了@autoreleasepool,但似乎没有用.此外,我已经尝试在不同的位置停止for循环,以查看内存是否在完成后释放,但事实并非如此.

此方法在for循环内调用,并接收图像URL和短名称.它已经在后台线程和主线程中尝试了相同的结果(内存方面).

-(void)saveImage:(NSString*)image name:(NSString*)imageName{     
    int k = 0;
    for (int j = 0; j < [imageName length]; j++) {
        if ([imageName characterAtIndex:j] == '/') {
            k = j;
        }
    }if (k != 0) {
        imageName = [imageName substringFromIndex:k+1];
    }    

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", imageName]]; 

    if ([fileManager fileExistsAtPath:fullPath]) {
        [fileManager removeItemAtPath:fullPath error:nil];
    }

    NSURL *url = [NSURL URLWithString:image];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url];
    NSLog(@"Saved: %d:%@", [fileManager createFileAtPath:fullPath contents:data attributes:nil], url); 
    data = nil;
}  


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[[UIApplication sharedApplication] setStatusBarHidden:YES];
[self.window makeKeyAndVisible];
return YES;
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
NSLog(@"mem warning, clearing cache");
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
Run Code Online (Sandbox Code Playgroud)

分配

Rob*_*Cat 5

根据您的屏幕截图,我认为问题在于NSURL缓存而不是实际的NSData对象.你能尝试以下方法吗?

在Application Delegate中,设置初始URL缓存:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Do initial setup
    int cacheSizeMemory = 16*1024*1024; // 16MB
    int cacheSizeDisk = 32*1024*1024; // 32MB
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
    [NSURLCache setSharedURLCache:sharedCache];

    // Finish the rest of your didFinishLaunchingWithOptions and head into the app proper
}
Run Code Online (Sandbox Code Playgroud)

将以下内容添加到您的应用程序委托:

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}
Run Code Online (Sandbox Code Playgroud)

将创建一个缓存文件:"Library/Caches/your_app_id/nsurlcache"

Apple示例的链接位于:URL Cache

代码未经过测试但是这个(或类似的东西)应该排序你的问题+加上你可以试验缓存大小.

你能用这段代码发布Allocations的另一个截图吗?我希望看到内存使用停止增长并趋于平缓.