gpt4 book ai didi

objective-c - 创建一个自动释放池并在不同的上下文中耗尽它

转载 作者:搜寻专家 更新时间:2023-10-30 20:10:58 26 4
gpt4 key购买 nike

在 View Controller 中,我有一个 UIImageView 作为 subview 。在 willRotateToInterfaceOrientation 上,我将其替换为不同的 UIImageView。如果两者同时在内存中,它有时会因为图像非常大而导致崩溃。所以我想确保在制作新的之前完全释放第一个。当我对其调用 removeFromSuperview 时,我认为它基本上是在稍后的某个时候自动释放,但我需要立即释放它。

所以看起来我在创建 UIImageView 时需要我自己的自动释放池,然后在 removeFromSuperview 调用之后调用 willRotateToInterfaceOrientation 时耗尽它。但是文档说:

An autorelease pool should always be drained in the same context (such as the invocation of a method or function, or the body of a loop) in which it was created. Autorelease pools are used “inline.” There should typically be no reason why you should make an autorelease pool an instance variable of an object.

那么“正确”的方法是什么?

最佳答案

最好的办法是优化您的图像。必须在同一上下文中分配和释放自动释放池。您应该做的第一件事是尝试减小图像的大小。如果它们是 png 的,请尝试 pngcrush。如果图像仍然很大,请考虑使用 mmap 一次加载部分图像。

Performance Tuning

Impose size limits on resources.
Avoid loading a large resource file when a smaller one will do. Instead of using a high-resolution image, use one that is appropriately sized for iOS-based devices. If you must use large resource files, find ways to load only the portion of the file that you need at any given time. For example, rather than load the entire file into memory, use the mmap and munmap functions to map portions of the file into and out of memory. For more information about mapping files into memory, see File-System Performance Guidelines.

关于objective-c - 创建一个自动释放池并在不同的上下文中耗尽它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com