gpt4 book ai didi

ios - 使用 ARC 加载许多 .jpg 文件时收到内存警告和崩溃

转载 作者:行者123 更新时间:2023-11-28 20:36:28 27 4
gpt4 key购买 nike

我的内存有问题。我用弧线。我正在将 .jpg 加载到我 View 中的 UIImageView。

Code showing example situation:

//.h file

@interface myClass : UIViewController
{
IBOutlet UIImageView * back;
}
// don' t have @property
// back is connected in .xib

//.m file
-(void) viewDidLoad {
UIImage * myImg =[UIImage imageNamed:imgName];

back.image=myImg;
}

-(void) returnToPrevoriousView {
[self removeFromSuperview];
back.image = nil;
}

问题是我从另一个 View 来到这个 View 并且 imgName 可以有很多值。例如,我可以来 myClass 六次然后我有日志:收到内存警告 和我的应用程序崩溃。

我认为 myImg 没有从内存中释放?我不知道怎么修? JPG 大约有 100kb。

附言。当我加载 .png 时,尽管 .png 图像通常有 600kB,但它仍然有效(速度也更快)。但我认为它仍然不释放内存?

最佳答案

如果您确定该图像不会在您的应用中再次使用,请不要使用 UIImage imageNamed:imageNamed: 使用将使用额外内存的内部缓存机制。

来自UIImage Class Reference :

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

而不是使用 UIImage imageWithContentsOfFile: 用于一次性图像,因为它们没有被缓存。

注意:只有 imageNamed: 会自动加载视网膜版本 (@2x)。

关于ios - 使用 ARC 加载许多 .jpg 文件时收到内存警告和崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10331617/

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