1

In my app, i have one method loadImage.
The loadImage method use to add new images into imageView.

-(void)loadimage:(int)index
{
  ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width*index, 0, self.view.frame.size.width, self.view.frame.size.height-88)]; // -88(Upper+Lower)

  NSString *imageNameStr = [NSString stringWithFormat:@"%@",[arrImages objectAtIndex:index]];
  ImageView.image=[UIImage imageNamed:imageNameStr];

  [ScrollView addSubview:ImageView];
}

After adding images, i want to remove old images.
The removeImage method use to remove old images from imageView.

How can i create removeImages method to remove selected image?

Night Hunter
  • 123
  • 1
  • 2
  • 13
  • 2
    If `[arrImages objectAtIndex:index]` is already an `NSString` (I suppose it is), then why are you adding another call to `+ [NSString stringWithFormat:]`? –  Feb 17 '13 at 21:15

3 Answers3

0

First, you need to keep around a list of image subviews. After that , you can call removeFromSuperview in the ones you want to remove from the scroll view.

Declare an NSMutableArray to hold the UIImageViews.

First, add this to your loadImage method at the end:

[arrayOfUIImageViews addObject:ImageView]

Then, to remove the image at index 0, do this:

[[arrayOfUIImageViews objectAtIndex:0] removeFromSuperview]
[arrayOfUIImageViews removeObjectAtIndex:0]
Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • i have no idea in removing selected image. I mean when i reach index2, i want to remove image from index0. I only want to take index1, index2 and index3 on memory.How can i remove index0 when i reach index2? – Night Hunter Feb 17 '13 at 21:23
0

you can put unique tag number to trace which UIImageView you want to delete. In your loadImage: method:

ImageView.tag = index + numberOffset; 

numberOffset is integer value that you know you are not going to use, so that you can avoid collision

then, to remove image

-(void)removeImage:(int)tagIndex
{
    UIImageView *imgView = (UIImageView *)[ScrollView viewWithTag:tagIndex];
    if (imgView != nil) 
    {
       [imgView removeFromSuperview];
    }
}

by calling removeImage:index + numberOffset

Hope that helps

Alphapico
  • 2,893
  • 2
  • 30
  • 29
0

You can set the image view to nil, it is the easiest way to clear it.

ImageView.image = nil;

As you can note in this answer as well. Removing image from UIImageView

Community
  • 1
  • 1
Lion789
  • 4,402
  • 12
  • 58
  • 96