Prakash Desai
Prakash Desai

Reputation: 511

Removing UIImageView From Button

This code i use for the purpose

 if(editedImage !=nil)
{
    [selectionView1 removeFromSuperview];

    UIImageView *selectionView1 = [[UIImageView alloc] initWithImage:editedImage.image];

    [_buttonScroll addSubview:selectionView1];
}

else{
[_buttonScroll addSubview:selectionView1];
}

i did in way when first time it executes then editedImage is nil so it goes in else then later on editedImage gets images like below

    -(void)imagePickerController:(UIImagePickerController *)picker
      didFinishPickingImage : (UIImage *)image
                 editingInfo:(NSDictionary *)editingInfo
{

      NSLog(@"%@",image);
  editedImage   = [[UIImageView alloc] initWithImage: image];
    NSLog(@"%@",editedImage);
    // editedImage is UIImageView

    [picker dismissModalViewControllerAnimated:YES];
}

so it get filled and when second time my first code execute then editedImage is not null and it goes inside of if(editedImage !=nil) condition and for that now i want to delete old image which was on button and add this new image which i got from edited image

so the problem is old image is not getting removed what am i doing wrong can you please suggest me something

Upvotes: 0

Views: 233

Answers (3)

DonnaLea
DonnaLea

Reputation: 8653

What you need to do is keep track of the image that you are adding. You can either keep a property variable to the image that is being added, but a better option is to just set the tag of the image that gets added. If you change your first if statement to as follows:

//set a tag value to use (defined at the top of your file outside the @implementation)
#define PREVIOUS_VIEW_TAG 999

if(editedImage !=nil)
{
    UIView* previousImageView = [_buttonScroll viewWithTag:PREVIOUS_VIEW_TAG];
    [previousImageView removeFromSuperview];

    UIImageView *selectionView1 = [[UIImageView alloc] initWithImage:editedImage.image];
    selectionView1.tag = PREVIOUS_VIEW_TAG;

    [_buttonScroll addSubview:selectionView1];
}

else{
    selectionView1.tag = PREVIOUS_VIEW_TAG;
    [_buttonScroll addSubview:selectionView1];
}

This should let you keep track of the view that you are adding. The value of the PREVIOUS_VIEW_TAG could be anything, I just picked 999.

Also keeping in mind what @Bhargavi suggested, using [_buttonScroll setBackgroundImage:selectionView1] and then removing with [_buttonScroll setBackgroundImage:nil]; (or just override by setting the new backgroundImage) would also work. However, to specifically answer your question, perhaps consider keeping track of the view with a tag.

Upvotes: 0

βhargavḯ
βhargavḯ

Reputation: 9836

-(void)imagePickerController:(UIImagePickerController *)picker
      didFinishPickingImage : (UIImage *)image
                 editingInfo:(NSDictionary *)editingInfo
{

    [_buttonScroll setImage:image forState:UIControlStateNormal];

    [picker dismissModalViewControllerAnimated:YES];
}

try this simply :)

Upvotes: 1

B.S.
B.S.

Reputation: 21726

Try:

-(void)imagePickerController:(UIImagePickerController *)picker
      didFinishPickingImage : (UIImage *)image
                 editingInfo:(NSDictionary *)editingInfo
{

  NSLog(@"%@",image);
  if(editedImage)
     [editedImage removeFromSuperview];

  editedImage   = [[UIImageView alloc] initWithImage: image];
    NSLog(@"%@",editedImage);
    // editedImage is UIImageView

    [picker dismissModalViewControllerAnimated:YES];
}

Upvotes: 0

Related Questions