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

时间:2014-01-06 09:31:42

标签: 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

2 个答案:

答案 0 :(得分:2)

重命名你的图像,如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;
    }
}

答案 1 :(得分:1)

解决方案取决于您要从中加载图片的位置。最简单的是添加到您的项目并显示这些图像。在这种情况下:

1)您必须将图像添加到项目中(文件/添加文件到..)

2)创建一个图像名称数组,不少于12

NSArray *my_image_names = @[@"image1.png", @"image2.png", @"image3.png",....<12 times>..];

3)修改代码:

img.image = [UIImage imageNamed:[my_image_names objectAtIndex:i]];