0

我用于此目的的代码

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

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

    [_buttonScroll addSubview:selectionView1];
}

else{
[_buttonScroll addSubview:selectionView1];
}

我第一次执行时这样做了,然后editedImage为nil,所以它进入else然后稍后editedImage得到如下图像

    -(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];
}

所以它被填满,当我第二次执行我的第一个代码时,editedImage 不为空,它进入 if(editedImage !=nil) 条件,为此,现在我想删除按钮上的旧图像并添加这个新图像我从编辑后的图像中得到

所以问题是旧图像没有被删除我做错了什么你能给我一些建议吗

4

3 回答 3

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

    [_buttonScroll setImage:image forState:UIControlStateNormal];

    [picker dismissModalViewControllerAnimated:YES];
}

试试这个简单:)

于 2013-04-12T05:29:20.237 回答
0

尝试:

-(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];
}
于 2013-04-12T05:06:49.993 回答
0

您需要做的是跟踪您正在添加的图像。您可以为要添加的图像保留一个属性变量,但更好的选择是只设置要添加的图像的标签。如果您将第一个 if 语句更改为如下:

//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];
}

这应该让您跟踪您正在添加的视图。PREVIOUS_VIEW_TAG 的值可以是任何值,我刚刚选择了 999。

还要记住@Bhargavi 的建议,使用 [_buttonScroll setBackgroundImage:selectionView1] 然后使用 [_buttonScroll setBackgroundImage:nil] 删除;(或者只是通过设置新的 backgroundImage 覆盖)也可以。但是,要具体回答您的问题,也许可以考虑使用标签跟踪视图。

于 2013-04-12T05:44:46.043 回答