UIImageView的多个图像设置

时间:2022-09-25 16:47:29

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.

最近我一直试图通过使用已经分配的UIImageViews和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.

我尝试做的是维护所有UIImages的地图 - 每个UIImages都是唯一的,并根据游戏引擎的需求将每个UIImage重新分配给几个UIImageViews。随着游戏的进行,我从显示中删除了一些UIImageViews,后来可能会使用不同的预加载UIImages重用它们。

The first time I create the UIImageView I do something like:

我第一次创建UIImageView时,我做了类似的事情:

m_ImageView = [[UIImageView alloc] initWithImage : initialImg];

m_ImageView = [[UIImageView alloc] initWithImage:initialImg];

[m_ParentView addSubview: m_ImageView];

[m_ParentView addSubview:m_ImageView];

And the following times I simply switch the image:

以下时间我只需切换图像:

[m_ImageView.image release];

[m_ImageView setImage otherSavedImg];

[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.

问题是在切换之后,显示的新图像大小与初始图像大小相同。我通过每次重新创建UIImageView来绕过这个问题(这似乎是浪费时间),但我想知道为什么会发生这种情况以及如何防止它。

Thanks,

1 个解决方案

#1


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.

UIImageView的文档说:“此方法调整接收器的帧以匹配指定图像的大小。”当调用“setImage:”时,你使用一个简单的属性设置器。没有进行额外的重新计算。如果需要重新调整UIImageView框架的大小,则必须手动完成。

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创建一个类别:

UIImageView+Resizing.h:

@interface UIImageView (Resizing)

- (void) setImage:(UIImage *)newImage resize:(BOOL)shouldResize;

@end

UIImageView+Resizing.m:

@implementation UIImageView (Resizing)

- (void) setImage:(UIImage *)newImage resize:(BOOL)shouldResize {
  [self setImage:newImage];
  if (shouldResize == YES) {
    [self sizeToFit];
  }
}

@end

Now, whenever you need to change an image and resize the imageView, simply #import UIImageView+Resizing.h and use:

现在,无论何时需要更改图像并调整imageView的大小,只需#import UIImageView + Resizing.h并使用:

[myImageView setImage:anImage resize:YES];

#1


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.

UIImageView的文档说:“此方法调整接收器的帧以匹配指定图像的大小。”当调用“setImage:”时,你使用一个简单的属性设置器。没有进行额外的重新计算。如果需要重新调整UIImageView框架的大小,则必须手动完成。

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创建一个类别:

UIImageView+Resizing.h:

@interface UIImageView (Resizing)

- (void) setImage:(UIImage *)newImage resize:(BOOL)shouldResize;

@end

UIImageView+Resizing.m:

@implementation UIImageView (Resizing)

- (void) setImage:(UIImage *)newImage resize:(BOOL)shouldResize {
  [self setImage:newImage];
  if (shouldResize == YES) {
    [self sizeToFit];
  }
}

@end

Now, whenever you need to change an image and resize the imageView, simply #import UIImageView+Resizing.h and use:

现在,无论何时需要更改图像并调整imageView的大小,只需#import UIImageView + Resizing.h并使用:

[myImageView setImage:anImage resize:YES];