从图像阵列动态添加图像到imageview

时间:2014-08-10 16:11:10

标签: ios image uiimage load

我可以动态添加标签和文本字段,但除非我使用图像的实际名称,否则图像不会显示。

这很有效     UIImage * individualTopImage = [UIImage imageNamed:@“photo1.png”];

我需要使用for循环并显示数组中的所有图像。

 //lets create  views
NSInteger numberTopOfViews = topImages.count;
for (int i = 0; i < numberTopOfViews; i++) {

    //set the origin of the sub view
    CGFloat myOrigin = i * self.view.frame.size.width;

    //create the sub view and allocate memory
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //set the background to white color
    myView.backgroundColor = [UIColor whiteColor];

    //create a label and add to the sub view
    CGRect myFrame = CGRectMake(10.0f, 10.0f, 200.0f, 25.0f);
    UILabel *myLabel = [[UILabel alloc] initWithFrame:myFrame];
    myLabel.text = [NSString stringWithFormat:@"This is page number %d", i];
    myLabel.font = [UIFont boldSystemFontOfSize:16.0f];
    myLabel.textAlignment =  NSTextAlignmentLeft;
    [myView addSubview:myLabel];

    //create a text field and add to the sub view
    myFrame.origin.y += myFrame.size.height + 10.0f;
    UITextField *myTextField = [[UITextField alloc] initWithFrame:myFrame];
    myTextField.borderStyle = UITextBorderStyleRoundedRect;
    myTextField.placeholder = [NSString stringWithFormat:@"Enter data in field %i", i];
    myTextField.tag = i+1;
    [myView addSubview:myTextField];

    //create a uiimage and add to sub view
    myFrame.origin.y +=myFrame.size.height +50.f;
    //UIImage *individualTopImage = [UIImage imageNamed:@"photo1.png"];
    UIImage *individualTopImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[topImages objectAtIndex:i]]];
    UIImageView *topImageView = [[UIImageView alloc] initWithImage: individualTopImage];

    [topImageView setFrame:CGRectMake(0, 0, myFrame.size.width,myFrame.size.height )];
    [myView addSubview:topImageView];


    //set the scroll view delegate to self so that we can listen for changes
    self.myScrollView.delegate = self;
    //add the subview to the scroll view
    [self.myScrollView addSubview:myView];
}

0 个答案:

没有答案