如何在iOS中的Scrollview中添加图像?

时间:2014-11-11 09:32:53

标签: ios iphone uiscrollview

我是iOS开发中的新手我想在我编写代码时向UIScrollView添加一个图像数组然后它只显示数组的最后一个图像到UIScrollview.i不明白什么是问题请给我解决方案

我为那个

编写代码
 for(int index=0; index < [self.imagesa count]; index++)
{
    NSDictionary *dict=[self.imagesa objectAtIndex:index];
    NSString *image=[dict valueForKey:@"link"];
    self.zoomImage.bounds=CGRectMake(0, 0, self.zoomScroll.frame.size.width +10.0f, self.zoomScroll.frame.size.height);
    self.zoomImage.frame=CGRectMake(index * self.zoomScroll.frame.size.width, 0, self.zoomScroll.frame.size.width, self.zoomScroll.frame.size.height);
    [self.zoomImage setUserInteractionEnabled:YES];
    [self.zoomImage setMultipleTouchEnabled:YES];
    [self.zoomImage sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
    [self.zoomImage setUserInteractionEnabled:TRUE];
    [self.objectarray insertObject:self.zoomImage atIndex:index];
    CGSize scrollViewSize=CGSizeMake(self.zoomScroll.frame.size.width*[self.objectarray count], self.zoomScroll.frame.size.height);
    [self.zoomScroll setContentSize:scrollViewSize];
    [self.zoomScroll addSubview:self.zoomImage];
    [self.zoomScroll addSubview:[self.objectarray objectAtIndex:index]];
}

当我制作本地图像视图时,显示该代码为this link shows my old code 它正在工作,但当我为此编写代码时,只显示最后一个数组图像,请给我解决方案。我知道这个问题很多次,但我没有得到解决方案。

2 个答案:

答案 0 :(得分:1)

您应该创建新的imageView。 for(int index=0; index < [self.imagesa count]; index++) {UIImageView *imageView = [[UIImageView alloc] init……]}而不是始终使用self.zoomImage

答案 1 :(得分:0)

你可以试试这个:

    for(int index=0; index < [self.imagesa count]; index++)
    {
        NSDictionary *dict=[self.imagesa objectAtIndex:index];
        NSString *image=[dict valueForKey:@"link"];
        UIImageView img=[[UIImageView alloc] init]; // allocate new object
        img.bounds=CGRectMake(0, 0, self.zoomScroll.frame.size.width +10.0f, img.frame.size.height);
        img.frame=CGRectMake(index * self.zoomScroll.frame.size.width, 0, self.zoomScroll.frame.size.width, self.zoomScroll.frame.size.height);
        [img setUserInteractionEnabled:YES];
        [img setMultipleTouchEnabled:YES];
        [img sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
        [img setUserInteractionEnabled:TRUE];
        [self.objectarray insertObject:img atIndex:index];
        [self.zoomScroll addSubview:img];
    }
    CGSize scrollViewSize=CGSizeMake(self.zoomScroll.frame.size.width*[self.objectarray count], self.zoomScroll.frame.size.height);
    [self.zoomScroll setContentSize:scrollViewSize];