UiImageView.image = nil 的问题

发布于 2024-11-16 14:47:38 字数 1625 浏览 1 评论 0原文

.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;

这样 做该线不起作用,所有以前的图像仍然保留在那里,并且新图像与当前图像重叠

.h file

  @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 file

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

when i press the button for the first time it works fine(old images get replaced with new ones),but if i do it second time

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

this line wont work and all the previous images still remain there and new images are overlapping with the present ones

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-11-23 14:47:38

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

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

此时,变量中已经存储了对 UIImageView 的引用。您正在丢失对它的引用,并且由于您分配/初始化了它,您很可能也会泄漏它的内存。此外,您正在向视图层次结构添加新的 UIImageView,但不会删除旧的。

尝试这样的事情:

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

In the ImageSwitch method you are always creating new instances of UIImageView.

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

At this point, there already is a reference to UIImageView stored in the variable. You are losing your reference to it and since you alloc/inited it, you will most probably also leak its memory. Moreover, you are adding new UIImageViews to the view hierarchy, but you are not removing the old ones.

Try something like this:

[imageViewArray[j][i] removeFromSuperview];
[imageViewArray[j][i] release];
imageViewArray[j][i] = [[UIImageView alloc] initWithImage: image_namesTmp[0]];
美人迟暮 2024-11-23 14:47:38

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

-(void)ImageSwitch{

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

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

Second time you set your images you don't need to recreate the UIImageView objects. Your ImageSwitch method is better declared as,

-(void)ImageSwitch{

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

Just changing the images will suffice and that's what you need to do.

甜中书 2024-11-23 14:47:38

您需要一个 ImageView 的 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 实例,然后您可以仅删除该图像视图而不是所有子视图。
希望这有帮助。

You need an NSMutableArray of ImageViews. In your header declare the NSMutableArray:

NSMutableArray *_imageViewArray;

Then in your viewDidLoad method initialize the array and then populate it with your image views.

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

So thats the image view array populated, you may do it a different way but thats up to you.
To switch the views over what I would do is like so...

[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.

Thats all you would need to do. If you wanted to know which image view was being displauyed the maybe declare an instance of UIImageView in your header called _currentImageView and then you can remove just that image view instead of all subviews.
Hope this helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文