我在我的应用程序中有这段代码:
var newImage = // ...
if (imageView.Image != null && imageView.Image != newImage)
imageView.Image.Dispose ();
imageView.Image = newImage;
我有三个相关的问题:
- 它是否立即释放先前的
imageView.Image
占用的内存? - 如果是,有更干净的解决方案吗?
- 这是否与
NSAutoreleasePool
有关?
NSAutoreleasePool
,我希望如果我将赋值包装在专用池中,就不必编写此if
条件。我猜这不是这种情况吗?恐怕我不明白在MonoTouch中应该如何使用NSAutoreleasePool
,什么是draining以及它如何与Mono的GC配合。 - Dan AbramovNSAutoreleasePool
就像 ObjC(适用于 Apple 文档),它与 GC(仅涵盖托管引用)没有直接关系,因为它是一个本地引用,将被保存在池中。这也意味着它不能通过使用if
和调用Dispose
来解决问题。请参见 Rolf 的答案 https://dev59.com/IGPVa4cB1Zd3GeqP4ENF#10440506,其中举了一个需要使用NSAutoreleasePool
的示例,但是任何先前存在的分配给UIImageView.Image
的UIImage
(在该示例中)不会被释放,直到 GC 处理它(因此最好像您自己的示例一样,在其上调用Dispose
,如果不为空)。 - poupou