定义一个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]; }