iPad 的图像问题 - 内存问题

发布于 2024-09-11 16:35:26 字数 1329 浏览 1 评论 0原文

所以我在 iPad 上安装了应用程序,但由于内存问题而崩溃了。我认为这是因为我使用的是非常大的图像文件,所以我返回并缩小了它们,通过调整它们的大小然后使用 PNGCrush 基本上削减了大约 75% 的大小。

现在,至于运行程序,如果我的 4 个单独选项卡中的每一个都有一个背景图像,如果每次切换选项卡时将图像设置为 Null 是否会节省内存,还是应该保留它们设置?我有一个页面,上面有几十个图像,因为它们充当按钮,从我听到的情况来看,听起来我应该在不查看该页面时将它们全部清除。这是正确的吗?

现在,当我启动时,我会以一种“立即加载并完成它”的心态加载应用程序中所有内容的所有图像,尽管从我一直在阅读的内容来看,这会导致内存问题,因为没有没有太多内存可以使用。这是否意味着每次切换选项卡或视图时,我都想清除不可见的所有图像,然后在返回时重新加载它们?这会导致性能的提高吗?或者至少防止崩溃?我的程序在模拟器中运行,但是当我在 ipad 上运行它时,它就会爆炸=/

谢谢!

编辑:我正在使用 Monotouch 顺便说一句,因此一切都在 C# 中

UIImage BG = UIImage.FromFile("Images/Makes/explosion.png");
UIImage basic = UIImage.FromFile("Images/Models/camaro.png");
UIImage advance = UIImage.FromFile("Images/search.png");

AdvancedSearchButton.SetBackgroundImage(advance, UIControlState.Normal);
ImageSearchButton.SetBackgroundImage(basic, UIControlState.Normal);
MainBG.Image = BG;

BG.Dispose();
basic.Dispose();
advance.Dispose();

现在我知道在常规 C# dispose() 中实际上并没有“释放”内存,但我读到一些内容说它被转换为“release” 当它编译为 obj-c 时,实际上会释放这些对象。

另外,我想知道在不再查看各个按钮和图像后是否需要 dispose() 。我只是将图像设置为 NULL,但这给了我错误。

MainBG.Dispose();
AdvancedSearchButton.Dispose();
ImageSearchButton.Dispose();

非常感谢您的帮助!

EDIT2:

所以我只是尝试了上面的代码,背景图像和其他所有内容仍然存在,看起来好像没有任何内容被真正清除。建议?

So I installed my app on my ipad and it crashes due to a memory issue. I figured this was because I'm using really big image files so I went back and reduced them all and essentially cut out about 75% of their size by resizing them and then using PNGCrush.

Now, as for running the program, if I have a background image for each of my 4 individual tabs, would it save memory if I were to set the images to Null every time I switch tabs or should I leave them set? I have one page which has a couple dozen images on it as they act as buttons and from what I'm hearing it sounds like I should clear them all when I'm not viewing that page. Is that correct?

Right now when I boot up, I load all of the images for everything in the app in a sort of: "load it now and be done with it" mentality, though from what I've been reading that causes memory issues as there isn't much memory to use. Does this mean every time I switch tabs or views I want to clear all the images from the ones that aren't visible and then reload them when we go back to them? Would this cause an increase in performance? Or at least prevent crashes? My program works in the simulator but when I run it on my ipad it just explodes =/

Thanks!

EDIT: I'm using Monotouch BTW hence everything is in C#

UIImage BG = UIImage.FromFile("Images/Makes/explosion.png");
UIImage basic = UIImage.FromFile("Images/Models/camaro.png");
UIImage advance = UIImage.FromFile("Images/search.png");

AdvancedSearchButton.SetBackgroundImage(advance, UIControlState.Normal);
ImageSearchButton.SetBackgroundImage(basic, UIControlState.Normal);
MainBG.Image = BG;

BG.Dispose();
basic.Dispose();
advance.Dispose();

Now I know in regular C# dispose() doesn't actually "free" memory, but I read something that says that it gets converted to a "release" when it compiles over to obj-c so that would essentially be freeing those objects.

Also, I'm wondering if I would need to dispose() of the individual buttons and the image after I'm no longer viewing them. I was just setting the image to NULL but that gave me errors.

MainBG.Dispose();
AdvancedSearchButton.Dispose();
ImageSearchButton.Dispose();

Thanks so much for the help!

EDIT2:

So I just tried the above code and the background images and everything else are still there and appears as if nothing is actually getting cleared. Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女 2024-09-18 16:35:26

iPhone 没有虚拟内存,也没有垃圾回收功能。因此,一旦某些内容加载到内存中,它就会保留在内存中,直到您的代码显式释放它。如果你不使用某些资源,你绝对应该尽快清理它们。

另外,您应该听取来自 iOS 的低内存警告,这是您的代码进行一些内部清理的另一个机会。

The iPhone does not have virtual memory and it does not have garbage collection. So once something is loaded in the memory it stays in the memory until your code explicitly releases it. If you are not using some resources, you should definitely clean them up as soon as possible.

Also, you should listen to low memory warnings from iOS, which is another opportunity for your code to do some internal clean up.

一抹微笑 2024-09-18 16:35:26

首先,延迟加载您的资源,除非您有正当理由不这样做。其次,我不确定你的图像有多大,但是对于大图像(本质上是用于背景等的图像),一般来说,我会将它们切成块,然后加载它们,你再一次猜到了,因为他们需要懒惰。

你应该做的是正确处理你的内存警告。取消分配任何不是绝对关键的资源,即其他选项卡中其他视图上的项目或隐藏内容。您可以在需要时再次加载它们。

您还应该使用 Instruments 来查看是否存在泄漏(我希望您已经这样做了),检查您的程序以查看是否可以在内存不足的情况下将一些缓存的资源保存到磁盘等。

First off, lazy load your resources unless you have a justifiable reason not to. Secondly, I'm not sure how big your images are, but on large images (ones that are by nature intended for things like backgrounds, or otherwise), generally speaking, I will chop them up into chunks, and load them, again you guessed it, lazily as they're needed.

What you should do is handle your memory warnings properly. Deallocate any resources that are not absolutely critical -- i.e., items on other views in other tabs, or hidden content. You can load them again when you have to.

You should also take a look at using Instruments to find out if you leak (I'm hoping you've already done this), examine your program to see if you can persist some cached resources to disk during low memory situations, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文