
此方法在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; // 4MBint cacheSizedisk = 32*1024*1024; // 32MBNSURLCache *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];}@H_502_4@解决方法 根据您的屏幕截图,我认为问题在于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} 将以下内容添加到您的应用程序委托:
- (voID)applicationDIDReceiveMemoryWarning:(UIApplication *)application { [[NSURLCache sharedURLCache] removeAllCachedResponses];} 将创建一个缓存文件:“library / Caches / your_app_ID / nsurlcache”
Apple示例的链接位于:URL Cache
代码没有经过测试,但是这个(或类似的东西)应该排序你的问题加上你可以试验缓存大小.
你能用这段代码发布Allocations的另一个截图吗?我希望看到内存使用停止增长并趋于平缓.
@H_502_4@ @H_502_4@ @H_502_4@ @H_502_4@ 总结以上是内存溢出为你收集整理的从服务器下载和保存大量图像时的iOS内存问题(ARC)全部内容,希望文章能够帮你解决从服务器下载和保存大量图像时的iOS内存问题(ARC)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)