UIImageView 的图像的多重设置
最近,我一直在尝试通过使用已分配的 UIImageViews 和 UIImages 来最大限度地减少分配和复制资源作为图像所花费的时间,从而节省渲染时间。
我尝试做的是维护所有 UIImage 的映射 - 每个 UIImage 都是唯一的,并根据游戏引擎的要求将每个 UIImageView 重新分配给多个 UIImageView。 随着游戏的进行,我从显示中删除一些 UIImageView,稍后可能会通过不同的预加载 UIImage 重用它们。
我第一次创建 UIImageView 时会执行以下操作:
m_ImageView = [[UIImageView alloc] initWithImage :initialImg];
[m_ParentView addSubview: m_ImageView];
接下来的时间我只是切换图像:
[m_ImageView.image发布];
[m_ImageView setImage otherSavedImg];
问题是切换后,显示的新图像尺寸与初始图像尺寸相同。 我通过每次重新创建 UIImageView 来绕过这个问题(这似乎是浪费时间),但我想知道为什么会发生这种情况以及如何防止它。
谢谢,
Recently I've been trying to save on render time by minimizing the amount of time spent on allocation and duplication of resources as images by using an already allocated UIImageViews and UIImages.
What I try to do is maintain a map of all UIImages - each one unique, and reassign each one to to several UIImageViews, by demand from the game engine.
As the game progresses I remove some UIImageViews from display, and later might reuse them with different preloaded UIImages.
The first time I create the UIImageView I do something like:
m_ImageView = [[UIImageView alloc] initWithImage : initialImg];
[m_ParentView addSubview: m_ImageView];
And the following times I simply switch the image:
[m_ImageView.image release];
[m_ImageView setImage otherSavedImg];
The problem is that after the switch, the new image size as displayed is identical to the initial image size.
I bypassed the problem by recreating the UIImageView every time (which seems like a waste of time), but I am wondering both why this happens and how can I prevent it.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UIImageView 的文档说“此方法调整接收器的框架以匹配指定图像的大小。” 当调用“setImage:”时,您使用一个简单的属性设置器。 不进行额外的重新计算。 如果您需要重新调整 UIImageView 框架的大小,则必须手动执行此操作。
从您提供的背景来看,这似乎是您经常做的事情。 因此,我建议为 UIImageView 创建一个类别:
UIImageView+Resizing.h:
UIImageView+Resizing.m:
现在,每当您需要更改图像并调整 imageView 大小时,只需 #import UIImageView+Resizing.h 并使用:
The docs for UIImageView say "This method adjusts the frame of the receiver to match the size of the specified image." When calling "setImage:", your using a simple property setter. No extra recalculation is done. If you need to readjust the size of the UIImageView's frame, you'll have to do so manually.
Judging from the context you gave, this seems like it'll be something you'll be doing somewhat frequently. As such, I recommend creating a category for UIImageView:
UIImageView+Resizing.h:
UIImageView+Resizing.m:
Now, whenever you need to change an image and resize the imageView, simply #import UIImageView+Resizing.h and use: