将数组中的图像显示到UIImageView

时间:2011-08-18 07:46:09

标签: iphone arrays image uiimageview

我正在尝试将数组中的图像显示到uiimageview,但它只显示最后一张图像。有什么建议吗?

for (int i = 0; i < [fileArr count]; i++) 
{
               NSArray *fileNameArr = [[fileArr objectAtIndex:i] componentsSeparatedByString:@"."];
check=line;
                NSString *msg = [textView.text stringByAppendingString:[NSString stringWithFormat:@"Opening image: %@\n",[fileArr objectAtIndex:i]]];

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

                line=line+1;

                [image removeFromSuperview];
                [image release];
                image = nil;
                //specify the image path, open the image file with UIImage library
                NSString *imagePath = [NSString stringWithFormat:@"%@/%@",jfn1,[fileArr objectAtIndex:i]];
                NSString *imageFile = [NSHomeDirectory() stringByAppendingPathComponent:imagePath];
                NSString *pathExtension = [imageFile pathExtension];
                NSLog(@"----------------------------");
                NSLog(@"ImageFile: %@\n",imageFile);
                NSLog(@"File Extention: %@\n", pathExtension);
                NSLog(@"Interger value: %i\n",i);

                UIImage *myImage = [UIImage imageWithContentsOfFile:imageFile];
                NSLog(@"Image Width: %f", myImage.size.width);
                NSLog(@"Image height: %f", myImage.size.height);

                image = [[UIImageView alloc] initWithImage:myImage];
                image.contentMode = UIViewContentModeScaleAspectFit;
                [scrollView addSubview:image];

1 个答案:

答案 0 :(得分:2)

因为你总是

[image removeFromSuperview];

之前

[scrollView addSubview:image];
在for循环中

怎么可能不只是显示最后一个imageView?