UiImageView.image = nil的问题

时间:2011-06-22 08:38:23

标签: objective-c uiimageview

.h文件

  @interface AppViewController : UIViewController {
  ...
  UIImageView *imageViewArray[3][5];// not using any @property for this
  ...

 }
  ...
 #define FOR(j,q) for (int j = 0; j < q; j++)

.m文件

  ViewDidLoad{
     FOR(i,5){
    FOR(j,3) 
    {
        image_names[0] = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",j]];
        imageViewArray[j][i] = [[UIImageView alloc] initWithImage: image_names[0]];
        CGRect newFrame ; 
        newFrame = CGRectMake(60+(j*130), 110+(150*i),130,136); 
        imageViewArray[j][i].frame = newFrame;
        [self.view addSubview:imageViewArray[j][i]];
      }
    }                   
}

 //clear the previous images and call a method to add new
 -(IBAction)Change{
      FOR(i,5)
        FOR(p,3) 
           imageViewArray[p][i].image = nil;

      [self ImageSwitch];
}


 -(void)ImageSwitch{

           FOR(i,5){
        FOR(j,3) 
        {   
           image_namesTmp[0] = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",j+3]];
           imageViewArray[j][i] = [[UIImageView alloc] initWithImage: image_namesTmp[0]];
           CGRect newFrame ; 
                 newFrame = CGRectMake(60+(j*130), 110+(150*i),130,136); 
              imageViewArray[j][i].frame = newFrame;
          [self.view addSubview:imageViewArray[j][i]];
                    }
               }
           }

当我第一次按下按钮时它工作正常(旧图像被新图像替换),但如果我第二次这样做

        imageViewArray[p][i].image = nil;

这条线不会工作,所有以前的图像仍然存在,新图像与现有图像重叠

3 个答案:

答案 0 :(得分:1)

ImageSwitch方法中,您始终会创建UIImageView的新实例。

imageViewArray[j][i] = [[UIImageView alloc] initWithImage: image_namesTmp[0]];

此时,已经存在对变量中存储的UIImageView的引用。你正在失去对它的引用,因为你分配/插入它,你很可能也会泄漏它的内存。此外,您正在向视图层次结构添加新的UIImageViews,但您不会删除旧的UIImageView。

尝试这样的事情:

[imageViewArray[j][i] removeFromSuperview];
[imageViewArray[j][i] release];
imageViewArray[j][i] = [[UIImageView alloc] initWithImage: image_namesTmp[0]];

答案 1 :(得分:1)

第二次设置图像时,无需重新创建UIImageView个对象。您的ImageSwitch方法最好声明为

-(void)ImageSwitch{

    FOR(i,5){
        FOR(j,3) 
        {   
            imageViewArray[j][i].image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",j+3]];
        }
    }
}

只需更改图像就足够了,这就是你需要做的事情。

答案 2 :(得分:0)

你需要一个ImageViews的NSMutableArray。在标题中声明NSMutableArray:

NSMutableArray *_imageViewArray;

然后在viewDidLoad方法中初始化数组,然后用图像视图填充它。

_imageViewArray = [[NSMutableArray alloc]init];
for (int i =0; i < imageNames; i++) {
   UIImageView *tempImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imageNames objectAtIndex:i];
   [_imageViewArray addObject:tempImageView];
   [tempImageView release];
 }

这样就可以填充图像视图阵列了,你可以采用不同的方式,但这取决于你。 切换我会做的事情是这样的......

[self.view removeAllSubviews]; // gets rid of all previous subviews.
[self.view addSubview:[_imageViewArray objectAtIndex:3]];// could be any number. generate a random number if you want.

这就是你需要做的一切。如果您想知道哪个图像视图被置换,可能​​会在您的标题中声明一个名为_currentImageView的UIImageView实例,然后您可以只删除该图像视图而不是所有子视图。
希望这有帮助。