UIScrollview的一些用法,解决里面ImageView只添加不释放的问题

来源:互联网 发布:浙江华通云数据 编辑:程序博客网 时间:2024/05/14 14:10

定义一个UIScrollview后往里面添加多张图片使其滑动显示,可以这么写:

IBOutlet UIScrollview *parkScrollView;


parkScrollView.contentSize=CGSizeMake(1024*3, 768);    for (int i=0; i<4; i++) {        NSString *imgName=[[NSString alloc]initWithFormat:@"m_side0_%d",i];        UIImage *img1=[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:imgName ofType:@"png"]];        UIImageView *imgView1=[[UIImageView  alloc]initWithFrame:CGRectMake(1024*i, 0, 1024, 768)];        [imgView1 setImage:img1];        [parkScrollView addSubview:imgView1];        [imgName release];        [img1 release];        [imgView1 release];    }

这么写确实达到效果了,但是,每当执行这段代码时,scrollview里面就会add几个ImageView,它是一直占用着系统内存.

有一个解决办法是,得到里面的ImageView,分别把他们remove掉,再执行添加操作.


int l=[parkScrollView.subviews count];    UIView *subview;    while (l>0) {        subview=[parkScrollView.subviews  objectAtIndex:0];        [subview removeFromSuperview];        l=[parkScrollView.subviews count];    }


原创粉丝点击