Programmatically access image assets

后端 未结 3 1274
小鲜肉
小鲜肉 2020-12-06 23:22

My image assets catalog has a folder named \"Nature\" with hundreds of image files inside.

I would like to get an array of image file names under the \"Nature\" dire

3条回答
  •  温柔的废话
    2020-12-07 00:04

    I know there are 2 ways to get an array of image files.


    In assets use the same prefix for all your images and append an incrementing number:
    Image names with incrementing number
    then you can:

    for (int i = 1; i <= 6; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"bg_button%d", i]];
        NSLog(@"%@", image);
    }
    

    Create folder references rather than image assets

    When importing images choose create folder references
    Images in folder

    So we can get an array of image files using this snippet:

    NSString *directory = [NSString stringWithFormat:@"Intro"];
    NSArray *intros = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:directory];
    NSLog(@"%@", intros);
    

    Hope this did help you!

提交回复
热议问题