0

I am trying to display images from the array to the uiimageview, but it only shows the last image. Any advise?

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];
3
  • How and what do you fill in fileArr? If possible, post the code. Commented Aug 18, 2011 at 7:53
  • you are removing your imageview in every iteration. Why blame 'it' for showing only the last one??? Commented Aug 18, 2011 at 8:01
  • - (NSArray *) listAndPopulateFileArray:(NSString *)jfn1 { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *jFolder = [NSHomeDirectory() stringByAppendingPathComponent:jFolder]; NSArray *imageList = [fileManager contentsOfDirectoryAtPath:jobFolder error:nil]; return imageList; } Commented Aug 18, 2011 at 8:07

1 Answer 1

2

Because you are always

[image removeFromSuperview];

before you

[scrollView addSubview:image];

in the for loop.

How could it not be only showing the last imageView?

Sign up to request clarification or add additional context in comments.

3 Comments

No need to call removeFromSuperview before addSubView, because when calling addSubView, the the subview is automatically removed from its previous superview before added as subview of another view.
If you have 10 images to show, you need to create 10 imageViews for them. You also need to set different positions for these imageViews by you self, otherwise they will be covered by each other.
Issit possible after I create the imageviews, I remove the imageview programmatically, to ensure that they will not overlap?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.