如何删除重叠的uiimageview?

时间:2011-08-05 07:31:42

标签: iphone objective-c xcode

我试图改变imageview中的图像,但我似乎无法做到这一点。 我在下面显示的代码为每个要显示的图像创建了一个新的子视图。然而,图像只是重叠并在末尾显示重叠的图像,这是不应该的。

的.m:

for (int i = 0; i < [imageArr count]; i++) {

        NSArray *imageNameArr = [[imageArr objectAtIndex:i] componentsSeparatedByString:@"."];

            check=line;
            NSString *msg = [textView.text stringByAppendingString:[NSString stringWithFormat:@"Opening image: %@\n",[imageArr objectAtIndex:i]]];

            [textView performSelectorOnMainThread:@selector(setText:) withObject:msg waitUntilDone:NO];

               line=line+1;

            NSString *imagePath = [NSString stringWithFormat:@"%@/%@",jName,[imageArr objectAtIndex:i]];
            NSString *imageFile = [NSHomeDirectory() stringByAppendingPathComponent:imagePath];
            NSLog(@"------------------------------");
            NSLog(@"ImageFile: %@\n",imageFile);
            NSLog(@"Interger value: %i\n",i);

            UIImage *myImage = [UIImage imageWithContentsOfFile:imageFile];
            UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [scrollView addSubview:imageView];
            [imageView release];
.....
.....
}

如何单独显示图像而不重叠?

2 个答案:

答案 0 :(得分:2)

将这一行放在for循环中:

    UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage]; 
    [scrollView addSubview:imageView];

并使用它:

    UIImageView *imageView = [[UIImageView alloc] init];
    [scrollView addSubview:imageView];

并使用:

    [imageView setImage:[UIImage imageWithContentsOfFile:fullImgNm]];

内部循环。

答案 1 :(得分:0)

您可以在循环中创建imageview,并在循环内更改该imageview的源(Image)。 你在循环开始之前这样做,

UIImageView *imageView = [[UIImageView alloc] init];
[scrollView addSubview:imageView];

并在循环中添加这样的图像,

[imageView setImage:[UIImage imageWithContentsOfFile:imageFile]];