这是 UIImage 的有效使用吗?
假设我有一个名为 Person 的对象。 Person 内部加载了一个名为“person.png”的 UIImage。
在我的应用程序中,我创建了许多 Person 对象,在运行时创建甚至删除它们。
由于 Person 对象内部保留了对 UIImage 的引用,这是否意味着每个 Person 都有唯一的图像内存分配?
或者所有 Person 对象都指向图像的同一内存位置吗?
Let's say I have an object called Person.
Person internally loaded an UIImage called "person.png".
In my app i create many Person objects, creating and even deleting them at runtime.
Since a Person object internally has a retained reference to UIImage, does this mean each Person has a unique memory allocation for the image?
Or do all Person objects point to the same memory location for the image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,这取决于您如何获取 UIImage。
每次创建新的 Person 对象时是否都使用 +[UIImage imageNamed:] ?如果是这样,UIKit 会缓存该图像并返回对内存中单个副本的引用。您保留它以确保它保持加载状态,然后在引用完它后释放它,但在内存中只创建了一个 UIImage 对象。
另一方面,如果您调用 +[UIImage initWithData:] 或 [[UIImage alloc] initWithContentsOfFile:] 您每次都会在内存中创建一个新副本(并且您可能需要停止它:)
Well, it depends on how you're getting the UIImage.
Are you using +[UIImage imageNamed:] each time you create a new Person object? If so, UIKit caches the image and returns references to a single copy in memory. You retain it to make sure it stays loaded, then release it when you're done referencing it, but only one UIImage object is created in memory.
If, on the other hand, you're calling +[UIImage initWithData:] or [[UIImage alloc] initWithContentsOfFile:] you're creating a new copy in memory each time (and you probably need to stop that :)
如果您的
Person
实例使用retain
作为对UIImage
实例的引用,那么它就指向内存中某处该图像的一个实例。如果您使用
copy
而不是retain
,您将拥有一个唯一的UIImage
实例。考虑查看有关 内存的 Apple 参考资料管理。
If your
Person
instance usesretain
for its reference to aUIImage
instance, then it is pointing to one instance of that image somewhere in memory.If you use
copy
instead ofretain
, you will have a unique instance ofUIImage
.Consider taking a look at the Apple reference on memory management.