ios - 如何在以下代码中插入图像数组?

标签 ios arrays image gallery

我已经下载了 Gallery 示例代码,但我不知道如何使用它?它显示 12 张相同的图片,我想要不同的图片,我想要一个 imagesArray 而不是一张图片,我该怎么办?

以下是我的代码,

#import "ViewController.h"
#import "UIViewAnimationExplose.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.allViews = [[NSMutableArray alloc]init];

    int numberColumn = 0;
    int numberRaw = 0;

    for (int i = 0; i < 12; i++) {

        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(10 + numberColumn * 252, 10 + numberRaw * 248, 242, 238)];
        img.contentMode = UIViewContentModeScaleAspectFill;
        img.clipsToBounds = YES;
        img.userInteractionEnabled = YES;
        img.image = [UIImage imageNamed:@"becomeapanda_tumblr_com"];
        [self.view addSubview:img];

        [self.allViews addObject:img];

        numberColumn++;
        if (numberColumn > 2) {
            numberRaw++;
            numberColumn =0;
        }
    }

    self.exploseContainer = [[AnimationExploseContainer alloc] initWithViews:self.allViews];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最佳答案

重命名您的图像,如 img1、img2..... img12,然后像这样修改您的代码,

    for (int i = 0; i < 12; i++) {

    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(10 + numberColumn * 252, 10 + numberRaw * 248, 242, 238)];
    img.contentMode = UIViewContentModeScaleAspectFill;
    img.clipsToBounds = YES;
    img.userInteractionEnabled = YES;
    img.image = [UIImage imageNamed:[NSString stringWithFormat:@"img%d",i]];
    [self.view addSubview:img];

    [self.allViews addObject:img];

    numberColumn++;
    if (numberColumn > 2) {
        numberRaw++;
        numberColumn =0;
    }
}

关于ios - 如何在以下代码中插入图像数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20946812/

相关文章:

ios - XCode 11.5 升级后不显示 React-Native Image

python - 将数据框导出到图像中

ios - 如何在 NSString 中多次出现的特定字符后获取 NSString?

ios - CollectionView 单元格意外行为 swift

ios - UITextView,编辑时滚动?

java - 将自定义对象数组保存到文件并在java中再次读取?

javascript - 将对象数组转换为一个逗号分隔的对象

ios - 在 cellForRow 中访问 NSUserDefaults(在 :IndexPath) in Swift, 内存使用

c++ - 模板中的数组和指针

image - 将图像 (300 DPI) 写入 PDF,而不损失质量