Ios 如何从图像视图中删除选定图像?

Ios 如何从图像视图中删除选定图像?,ios,cocoa-touch,imageview,Ios,Cocoa Touch,Imageview,在我的应用程序中,我有一个方法loadImage。 loadImage方法用于将新图像添加到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)]; // -

在我的应用程序中,我有一个方法loadImage。 loadImage方法用于将新图像添加到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];
}
添加图像后,我想删除旧图像。 removeImage方法用于从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];
}

如何创建removeImages方法来删除选定的图像?

首先,您需要保留一个图像子视图列表。之后,您可以在要从滚动视图中删除的视图中调用removeFromSuperview

声明NSMutableArray以保存UIImageView

首先,在末尾将其添加到loadImage方法中:

[arrayOfUIImageViews addObject:ImageView]
然后,要删除索引0处的图像,请执行以下操作:

[[arrayOfUIImageViews objectAtIndex:0] removeFromSuperview]
[arrayOfUIImageViews removeObjectAtIndex:0]

您可以放置唯一的标记号来跟踪要删除的UIImageView。 在loadImage:方法中:

NumberOfSet是您知道不打算使用的整数值,这样可以避免冲突

然后,删除图像

-(void)removeImage:(int)tagIndex
{
    UIImageView *imgView = (UIImageView *)[ScrollView viewWithTag:tagIndex];
    if (imgView != nil) 
    {
       [imgView removeFromSuperview];
    }
}
通过调用removeImage:index+numberofset


希望对您有所帮助

您可以将图像视图设置为零,这是清除图像的最简单方法

ImageView.image = nil;
正如你在这个答案中所注意到的。

如果[arrImages objectAtIndex:index]已经是一个NSString,我想是的,那么为什么还要添加另一个对+[NSString stringWithFormat:]的调用?我不知道如何删除所选图像。我的意思是,当我到达index2时,我想从index0中删除图像。我只想在内存中获取index1、index2和index3。当我到达index2时,如何删除index0?