zl程序教程

您现在的位置是:首页 >  其它

当前栏目

imageNamed is evil[转]

is
2023-09-14 08:59:42 时间

imageNamed is evil

So a little while back we wrote about the confusing crashes on the device that we eventually figured out were caused by [UIImage imageNamed:] running out of memory when it tried to cache lots of big images. So we apparently solved the problem by making a thumbnail-sized image set and caching those, some 2.9 meg worth, whilst loading in the full sized ones uncached and only as needed.

Well, apparently turned out to be not good enough. See, it’s a few days later and we’ve finalized the design and got everything implemented and all seems to be good … except that it gets terminated without warning sooner or later. And quite often, Springboard terminates as well. Absolutely no correlation to any action or sequence in the program, no leaks, no out of bounds memory accesses, memory usage of the program barely a pittance. Yet, somehow, look at the console and you see system memory warnings scrolling by, you see it quitting background processes, and eventually quitting Springboard and/or the active application. Whilst that active application hums merrily along in blithe ignorance of the system crashing to the ground behind its back.

So apparently not only is 2.9 meg of images cached with +imageNamed enough to bring the iPhone OS to its knees, it’s not smart enough to, you know, actually do anything about it, like oh I don’t know, empty the cache or something?

It’s not like this is hard or anything, you can replicate the caching functionality precisely by declaring yourself an  NSMutableDictionary *thumbnailCache and populating it like

- (UIImage*)thumbnailImage:(NSString*)fileName
{
   UIImage *thumbnail = [thumbnailCache objectForKey:fileName];

   if (nil == thumbnail)
   {
      NSString *thumbnailFile = [NSString stringWithFormat:@"%@/thumbnails/%@.jpg", [[NSBundle mainBundle] resourcePath], fileName];
      thumbnail = [UIImage imageWithContentsOfFile:thumbnailFile];
      [thumbnailCache setObject:thumbnail forKey:fileName];
   }
   return thumbnail;
}

and if you do get a low memory issue, just [thumbnailCache removeAllObjects] and you’re good. But you know what? After replacing the various +imageNamed calls with this … not a single quibble anywhere, the whole 2.9 meg worth of cached thumbnails go right in there and not a single problem for, literally, hours.

(Not that there were any problems after those hours, we hasten to add; simply that, you know, if you think about it, there probably really isn’t a lot of point extending testing of an iPhone application much past the lifespan of a full battery charge…)

So the moral of the story is: DO NOT USE [UIImage imageNamed] for any significant amount of images. It is EVIL. It WILL bring down your application and/or Springboard, even when your application is putting along using just barely a nibble of memory on its own. Take the handful of lines above and implement your own cache!

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2011/11/18/2254653.html
编程中最难的就是命名?这几招教你快速上手 在项目中,从项目的创建到方法的实现,每一步都以命名为起点,我们需要给变量、方法、参数、类命名,这些名字出现在代码的每个角落,随处可见,混乱或错误的命名不仅让我们对代码难以理解,更糟糕的是,会误导我们的思维,导致对代码的理解完全错误。如果整个项目始终贯穿着好的命名,就能给阅读者一个神清气爽的开始,也能给阅读者一个好的指引。
带你深入React 18源码之:useMemo、useCallback和memo 在这篇文章中,我们将探讨useMemo、useCallback和memo的用法和区别,并通过源码分析来理解它们的工作原理,开整!
阿里云灵积模型服务Quick Start DashScope灵积模型服务以模型为中心,致力于面向AI应用开发者提供品类丰富、数量众多的模型选择,并为其提供开箱即用、能力卓越、成本经济的模型服务API。DashScope灵积模型服务依托达摩院等机构的优质模型,在阿里云基础设施之上构建。灵积服务4.11号刚刚开通公测,目前提供Paraformer语音识别API能力,后续通义千问也将通过该服务对外提供API能力。本文演示如何快速通过Python SDK接入服务。
ClickHouse创始人:融合数据库该“卷”的还是性能和速度 在刚刚结束的阿里云瑶池数据库峰会上,阿里云宣布与全球流行的开源分析型数据库 ClickHouse 正式签订战略合作协议,成为 ClickHouse 在中国独家的云服务提供商,并提供具备独有企业能力的 ClickHouse 版本。借此机会,王一鹏有幸独家专访了 ClickHouse 创始人兼 CTO Alexey Milovidov、阿里云数据库事业部 OLAP 产品部负责人林亮,围绕 ClickHouse 演进迭代的历程、双方此次合作的契机、当前数据库技术所面临的挑战和机遇,以及 OLAP 数据库未来发展趋势等问题展开深度对谈。
0基础也能搭建个人网站! 哈喽大家好 ,我是小宇。之前做了一期0成本搭建个人网站的教程,准确来说是0经济成本,反响还不错。但是毕竟是0成本,受限太多,很多小伙伴想学习如何在服务器上搭建网站。正好大学期间,简单的搭建过一些网站,我又做了一些功课,今天小宇就教大家0基础在服务器上搭建属于自己的网站!废话少说,正片开始。
阿里云宣布ECS最新主售实例降价40% 全栈云产品开放免费试用 阿里云宣布ECS最新主售实例降价40%  全栈云产品开放免费试用 阿里云推出对象存储预留空间产品 价格最多降低70%