UIImage ImageNamed 方法
在我的应用程序中,我将多个图像加载到 UIScrollView 上,并使用 Core Graphics 例程突出显示滚动视图的一部分。我在例程期间使用 CGImageRelease 和 CGContextRelease 来管理内存。
当我使用工具(分配)运行应用程序时,我发现内存消耗随着滚动视图的每次滑动而不断增加。这在某一时刻会导致应用程序变得非常慢。
为了加载不同的图像,我使用 UIImage ImageNamed 方法,我遇到过一些帖子,表明这不是一个好主意,因为该方法会导致自动释放图像,从而产生内存问题。我想知道我是否正在寻找错误的正确位置。可能在哪里寻找这种异常的内存消耗?
另外,使用仪器的分配,我只能看到内存的增加,是否可以查明这些分配发生的代码?
预先感谢您的帮助!
最好的 德克维
In my app, I am loading several images onto a UIScrollView and I highlight a portion of the scroll view using Core Graphics routine. I have used the CGImageRelease and CGContextRelease to manage the memory during the routines.
When I run the app using instruments (allocation), I see that the memory consumption keeps rising with every swipe of the scrollView. This at one point leads to the app becoming really slow.
For loading different images, I use the UIImage ImageNamed method, I have come across some posts indicating that this is not a good idea since the method results in autoreleased images which creates memory issues. I would like to know if I am looking a the right place for the error. What could be the possible place to look for this unusual memory consumption?
Also, using the allocation of Instruments, I can just see that increase in the memory, is it possible to pin point the code where these allocations happen?
Thanks in advance for your help!
Best
DKV
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这不是问题。任何不涉及您自己调用
alloc
、init
[WithSomethingOrOther:
] 和release
的操作都将获取自动发布的图像。问题是
imageNamed:
在将图像交给您后仍然拥有该图像。您从imageNamed:
获取的每个图像都会保留在该缓存中,并与该名称永久关联。它不仅仅是“加载此图像”方法;这是一种“加载此图像并使其永远保持活力*”的方法。*其中“永远”的意思是“直到我的过程结束”。
在仪器中。它会准确地告诉您正在创建的每种对象有多少,以及每种对象总共占用了多少内存,并且您可以对该列表进行排序以确定什么占用了内存。然后,您可以深入了解每个类和每个对象,以确定在需要对象后是什么使它们保持活动状态。
No, that's not the issue. Anything that doesn't involve you calling
alloc
,init
[WithSomethingOrOther:
], andrelease
yourself is going to get the image autoreleased.The problem is that
imageNamed:
continues to own the image after it hands it to you. Every image you obtain fromimageNamed:
stays in that cache, permanently associated with that name. It's not merely a “load this image” method; it's a “load this image and keep it alive forever*” method.*Where “forever” means “until the end of my process”.
In Instruments. It will tell you exactly how many of each kind of object you are creating, and how much total memory objects of each kind are occupying, and you can sort that list to determine what's eating up memory. Then you can drill down into each class and into each object to determine what's keeping objects alive after you need them.