【发布时间】:2015-08-08 00:40:45
【问题描述】:
我想在滚动视图中显示多个图像,当图像超过 70 时,应用程序将崩溃并显示收到的内存错误。我已从文档目录获取图像
我试过了-
UIScrollView *MyScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
int x=5,y=15;
for (int i = 0; i < imgarr.count; i++)
{
if(x<=211)
{
UIImage* imagePath = [UIImage imageWithContentsOfFile:[path_array objectAtIndex:i]];
imgView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 77, 75)];
imgView.image=imagePath;
imgeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
imgeBtn.frame=CGRectMake(x, y, 93, 110);
[imgeBtn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
imgeBtn.tag=i;
x+=103;
}
else
{
x=5;
y+=130;
UIImage* imagePath = [UIImage imageWithContentsOfFile:[path_array objectAtIndex:i]];
imgView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 77, 75)];
imgView.image=imagePath;
imgeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
imgeBtn.frame=CGRectMake(x, y, 93, 110);
[imgeBtn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
imgeBtn.tag=i;
x+=103;
}
[MyScroll addSubview:imgeBtn];
[MyScroll addSubview:imgView];
MyScroll.contentSize=CGSizeMake(320, y+120);
如何在滚动视图中显示多张图片?
【问题讨论】:
-
您能解释一下为什么要使用 if(x
-
Scrollview 并不是显示多个图像的更好方法。 UICollectionView 是显示图像的更好方法,因为它可重用。每次在 Scrollview 中分配内存时,应用程序都会崩溃或负载过大。
-
我尝试了 UIScrollView 和 UICollectionView 但结果是一样的。
-
使用 uicollection 视图代替 uiscroll 视图会更好,因为在 uicollection 视图中我们能够创建一个可重用的单元格。
-
@sohil 请以更好的方式消耗你的赏金,你现在正在消耗宽限期
标签: ios objective-c iphone crash