gpt4 book ai didi

ios - UIImageView 在不在 View 中时是否释放资源?

转载 作者:行者123 更新时间:2023-11-29 11:16:53 25 4
gpt4 key购买 nike

是否 UIImageView不在 View 中时自动释放图像资源?也许当应用收到内存警告时?

如果没有,我正在考虑编写一个这样做的子类(例如 SmartReleaseImageView ),以在特殊情况下使用,这将是有益的。我在考虑这些方面的行为:

  • 何时SmartReleaseImageView收到setImage消息,它向管理器注册自己,例如SmartReleaseImageViewManager .
  • 当应用收到内存警告时,SmartReleaseImageViewManager遍历其所有 SmartReleaseImageViews并检查它们中是否有任何一个未添加到父 View ,或检查它们中是否有一个框架由于超出主窗口的边界而被剔除。
  • 何时SmartReleaseImageView检测到它已进入 View 并已被释放,它会再次加载图像。所以也许,它需要有一个像setImageURL这样的方法。而不是 setImage首先。

这种行为是否已经内置到UIImageView中? ?或者它是否存在于任何地方的第 3 方版本中?还是思路有问题?

(我正在编写一个 UIImageView-heavy 应用程序,如果有一个全局解决方案可以帮助解决内存问题,而不是向我的 UIViewControllers 添加大量额外代码,那就太好了。)

编辑:目前的答案表明 UIViewController应该负责。这是真的,我想我的特殊情况是不典型的。在我的例子中,UIViewController 可以包含很多自定义 View (具有自己的层次结构),它不一定知道它们的结构,并且经常进入和退出 View 。因此,它很难知道何时释放其资源。但是,是的,也许我应该找到一种方法让它自己处理问题......

最佳答案

好吧,您实际上已经在 UIViewController 中实现了这种自动管理。由于 UIImageView 是 UIView 的子类,您的 View Controller 应该能够自动管理它。

Memory Management

Memory is a critical resource in iOS, and view controllers provide built-in support for reducing their memory footprint at critical times. The UIViewController class provides some automatic handling of low-memory conditions through its didReceiveMemoryWarning method, which releases unneeded memory. Prior to iOS 3.0, this method was the only way to release additional memory associated with your custom view controller class but in iOS 3.0 and later, the viewDidUnload method may be a more appropriate place for most needs.

When a low-memory warning occurs, the UIViewController class purges its views if it knows it can reload or recreate them again later. If this happens, it also calls the viewDidUnload method to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded with the nib file, objects created in your viewDidLoad method, and objects created lazily at runtime and added to the view hierarchy. Typically, if your view controller contains outlets (properties or raw variables that contain the IBOutlet keyword), you should use the viewDidUnload method to relinquish ownership of those outlets or any other view-related data that you no longer need.

来源:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

关于ios - UIImageView 在不在 View 中时是否释放资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9078541/

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