此代码我用于此目的
if(editedImage !=nil)
{
[selectionView1 removeFromSuperview];
UIImageView *selectionView1 = [[UIImageView alloc] initWithImage:editedImage.image];
[_buttonScroll addSubview:selectionView1];
}
else{
[_buttonScroll addSubview:selectionView1];
}
当我第一次执行时我做了,然后编辑了ImageImage是nil所以它进入else,然后在editImage上获取如下图像
-(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不为null并且它进入if(editedImage!= nil)条件,为此现在我想要删除按钮上的旧图像并添加此我从编辑过的图像中得到的新图像
所以问题是老图像没有被删除我做错了你能不能给我建议
答案 0 :(得分:1)
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[_buttonScroll setImage:image forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
}
试试这个:)
答案 1 :(得分: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];
}
答案 2 :(得分: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来覆盖)也可以。但是,要专门回答您的问题,可以考虑使用标记跟踪视图。