UiImageView.image = nil 的问题
.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
ImageSwitch
方法中,您始终创建UIImageView
的新实例。此时,变量中已经存储了对 UIImageView 的引用。您正在丢失对它的引用,并且由于您分配/初始化了它,您很可能也会泄漏它的内存。此外,您正在向视图层次结构添加新的 UIImageView,但不会删除旧的。
尝试这样的事情:
In the
ImageSwitch
method you are always creating new instances ofUIImageView
.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:
第二次设置图像时,无需重新创建
UIImageView
对象。您的ImageSwitch
方法最好声明为,只需更改图像就足够了,这就是您需要做的。
Second time you set your images you don't need to recreate the
UIImageView
objects. YourImageSwitch
method is better declared as,Just changing the images will suffice and that's what you need to do.
您需要一个 ImageView 的 NSMutableArray。在标头中声明 NSMutableArray:
然后在 viewDidLoad 方法中初始化该数组,然后用图像视图填充它。
这就是填充的图像视图数组,您可以采用不同的方式,但这取决于您。
要切换我要做的事情的视图就像这样......
这就是您需要做的所有事情。如果您想知道正在显示哪个图像视图,则可以在标头中声明一个名为 _currentImageView 的 UIImageView 实例,然后您可以仅删除该图像视图而不是所有子视图。
希望这有帮助。
You need an NSMutableArray of ImageViews. In your header declare the NSMutableArray:
Then in your viewDidLoad method initialize the array and then populate it with your image views.
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...
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.