简体   繁体   中英

iPhone: Removing Image from NSMutableArray

I am creating an app in which i am storing UIImage in NSMutableArray

    -(void)mainImageSelected: (id)sender
{    
    mainImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"image%d.png",[sender tag]]]];
    [myArray addObject:mainImageView];
}

Now, i want to display this Image where user touches. I am doing something like this:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];

    if ([currentObject isEqualToString:@"myObject"])
    {
            NSLog(@"Hello World");
            [self.view addSubview:[myArray lastObject]];
            [[myArray lastObject]setCenter:point];
    }
}

Till here, it is working fine, but the problem is that i want to remove this image when the user press undo button. For this, i am doing something like this:

-(IBAction)Undo
{
    [myArray removeLastObject];
}

It is removing the object from array, but the image is not deleted/removed. Anyone please solve this issue.

-(IBAction)decorationUndo
{
    [decorationArray removeLastObject];
    [self.view.subviews removeLastObject];
    [self setNeedsDisplay];
}

You should also remove the image from the view it is placed in before removing it from the array:

 -(IBAction)decorationUndo
{
  [[myArray lastObject] removeFromSuperview];
  [myArray removeLastObject];
}

In the code above, I am assuming that decorationArray is an array of UIImageView objects containing your images.

Befor removing it from the array first remove it from the view.

-(IBAction)Undo
{
    [[myArray lastObject] removeFromSuperview];
    [myArray removeLastObject];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM