简体   繁体   English

我可以自动创建UIImageView实例吗?

[英]Can I create instances of UIImageView automatically?

Now I create is as follow (my file.h): 现在我创建如下(我的file.h):

UIImageView *pic1, *pic2, *pic3, *pic4, *pic5, *pic6, *pic7, *pic8, *pic9, *pic10;

Then in my (file.m):

UIImageView *pic1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”picName.png”]];

UIImageView *pic2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”picName.png”]];
……

UIImageView *pic10 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”picName.png”]];

I need many instances of UIImageView (number triggered by other factors) of only one in this case picture. 在这种情况下,我只需要一个UIImageView的许多实例(由其他因素触发的数量)。

Is there any way to create multiple instances of UIImageView automatically in my file.m, somehow as follow?: 有什么方法可以在我的file.m中自动创建多个UIImageView实例,如下所示:

for (int x; (x=10); x++)
{
    UIImageView * pic[x] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myPic.png"]];
}

This example doesn't work but I'd like to show what I want to program. 这个例子不起作用,但是我想展示我想要编程的东西。

Of course you can - that is what the arrays are for: 当然可以-这就是数组的用途:

NSMutableArray *pics = [NSMutableArray array];
for (int i = 0 ; i != 10 ; i++) {
    [pics addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myPic.png"]]];
}

In case the name of the picture depends on the index, use NSString 's stringWithFormat to produce the name of the picture - for example, you can do it like this: 如果图片的名称取决于索引,请使用NSStringstringWithFormat生成图片的名称-例如,您可以这样操作:

NSMutableArray *pics = [NSMutableArray array];
for (int i = 0 ; i != 10 ; i++) {
    NSString *imgName = [NSString stringWithFormat:@"myPic%d.png"];
    [pics addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]]];
}

If the names of images follow a standard pattern you can just loop over the required amount and build the image name dynamically using the index. 如果图像名称遵循标准模式,则可以循环所需的数量,并使用索引动态构建图像名称。

If the names of the images do not follow a standard patten you can chuck them in an array and loop over them 如果图像名称未遵循标准图案,则可以将它们以阵列形式夹住并在它们上循环

NSArray *imageNames = @[
  @"alarm.jpg",
  @"bell.jpg",
  @"car.jpg"
];

NSMutableArray *imageViews = [[NSMutableArray alloc] init];

for (NSString *imageName in imageNames) {
  [imagesViews addObject:({
    [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
  })];
}

Completely optional - further reading 完全可选-进一步阅读

You could write yourself a simple category that abstracts this looping and collecting the results into a new array. 您可以为自己编写一个简单的类别,以对该循环进行抽象并将结果收集到一个新数组中。 This would allow you to simply write 这将使您只需编写

NSArray *imageNames = @[
  @"alarm.jpg",
  @"bell.jpg",
  @"car.jpg"
];

NSArray *imageViews = [imageNames pas_arrayByMappingWithBlock:^(id obj){
  return [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
}];

The category to do that would look something like this: 执行此操作的类别如下所示:

@interface NSArray (PASAdditions)

- (NSArray *)pas_arrayByMappingWithBlock:(id (^)(id obj))block

@end

@implementation NSArray (PASAdditions)

- (NSArray *)pas_arrayByMappingWithBlock:(id (^)(id obj))block
{
  NSMutableArray *result = [[NSMutableArray alloc] init];

  for (id obj in self) {
    [result addObject:block(obj)];
  }

  return [result copy];
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM