【问题标题】:Display images from array to UIImageView将图像从数组显示到 UIImageView
【发布时间】:2011-08-18 07:46:09
【问题描述】:

我正在尝试将数组中的图像显示到 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];

【问题讨论】:

  • 如何填写fileArr?如果可能,请发布代码。
  • 您将在每次迭代中删除您的图像视图。为什么责怪“它”只显示最后一个???
  • - (NSArray *) listAndPopulateFileArray:(NSString *)jfn1 { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *jFolder = [NSHomeDirectory() stringByAppendingPathComponent:jFolder]; NSArray *imageList = [fileManager contentsOfDirectoryAtPath:jobFolder error:nil];返回图像列表; }

标签: iphone arrays image uiimageview


【解决方案1】:

因为你一直都是

[image removeFromSuperview];

在你之前

[scrollView addSubview:image];

在 for 循环中。

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

【讨论】:

  • 不需要在addSubView之前调用removeFromSuperview,因为在调用addSubView时,子视图会自动从之前的父视图中删除,然后再添加为另一个视图的子视图。
  • 如果你有 10 张图片要展示,你需要为它们创建 10 个 imageViews。这些imageView也需要自己设置不同的位置,否则会互相覆盖。
  • 是否可以在创建图像视图后以编程方式删除图像视图,以确保它们不会重叠?
猜你喜欢
  • 1970-01-01
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
  • 2019-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多