Objective c 其他UIImageView NSMutableArray

Objective c 其他UIImageView NSMutableArray,objective-c,uiimageview,nsmutablearray,Objective C,Uiimageview,Nsmutablearray,我有5张图片,我想添加到阵列中 代码: 当我构建并运行这段代码时,我只看到数组的最后一张图片,只有pic5.png 不管索引是什么 当我在数组中使用NSString时,一切正常,但在数组中我使用了图像否(您每次都需要创建一个新的UIImageView。当前您只需创建一个UIImageView,并更改其图像五次。您已将同一图像视图对象的五个版本添加到数组中 大概是这样的: pic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 7

我有5张图片,我想添加到阵列中

代码:

当我构建并运行这段代码时,我只看到数组的最后一张图片,只有pic5.png 不管索引是什么


当我在数组中使用NSString时,一切正常,但在数组中我使用了图像否(

您每次都需要创建一个新的
UIImageView
。当前您只需创建一个
UIImageView
,并更改其图像五次。您已将同一图像视图对象的五个版本添加到数组中

大概是这样的:

pic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 75)];
pic.image = [UIImage imageNamed:@"pic1.png"];
pic.center = CGPointMake(100, 110);
[picArray addObject:pic];

// Note how a new image is created here
pic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 75)
pic.image = [UIImage imageNamed:@"pic2.png"];
pic.center = CGPointMake(100, 120);
[picArray addObject:pic];

// ...etc

是,此处仅显示最后一张图像,因为您只是在imageview中更改图像。 使用不同的帧拍摄不同的imageview,并将其添加到视图中

要在视图中添加图像,可以选择一个作为循环,并根据帧添加图像

for(int i=0;i<5;i++)
    {
    pic = [[UIImageView alloc] initWithFrame:CGRectMake(0, i*75, 50, 75)
    pic.image = [UIImage imageNamed:[NSString stringWithFormat:@"pic%d.png",i];
    pic.center = CGPointMake(100, 120);
    [self.view addObject:pic];

    }

for(int i=0;i每次添加新图片时,都必须重新初始化UIImageView

  pic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 75)
加在前面

pic.image = [UIImage imageNamed:@"pic5.png"];
pic.image = [UIImage imageNamed:@"pic4.png"];
pic.image = [UIImage imageNamed:@"pic3.png"];
pic.image = [UIImage imageNamed:@"pic2.png"];
pic.image = [UIImage imageNamed:@"pic1.png"];
在你的法典中,你应该遵守犹太教

pic.image = [UIImage imageNamed:@"pic5.png"];
pic.image = [UIImage imageNamed:@"pic4.png"];
pic.image = [UIImage imageNamed:@"pic3.png"];
pic.image = [UIImage imageNamed:@"pic2.png"];
pic.image = [UIImage imageNamed:@"pic1.png"];