gpt4 book ai didi

ios - 收到内存警告然后崩溃

转载 作者:技术小花猫 更新时间:2023-10-29 11:13:19 29 4
gpt4 key购买 nike

当图像超过 70 个时,我想在 ScrollView 中显示多个图像应用程序将崩溃并显示接收到的内存错误。我已经从文档目录中获取图像

我试过了-

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

如何在 ScrollView 中显示多张图片?

最佳答案

其他人提出的切换到 UICollectionView 的建议很好,但它忽略了您代码中的真正缺陷:UIImage 的方法 imageNamed: 将您所有的 70 张图片永久保存在内存中。根据文档:

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

如果您只将需要的图像加载到内存中,切换到此方法将防止崩溃。 UICollectionView 使这变得微不足道,因为您只能在 Collection View 要求您填写其数据源的 collectionView:cellForItemAtIndexpath: 方法中的单元格对象时加载图像。

关于ios - 收到内存警告然后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30456870/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com