简体   繁体   English

当使用ARC加载许多.jpg文件时,收到内存警告并崩溃

[英]Recived memory warning and crash when loading many .jpg files with ARC

I have problem with memory. 我的记忆有问题。 I use ARC. 我使用ARC。 I' m loading .jpgs to UIImageView on my view. 我正在将.jpgs加载到视图上的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;
}

The problem is that i come to this view from the other view and imgName can have many values. 问题是我从另一个视图进入此视图,并且imgName可以具有许多值。 I can come myClass for example six times then I have log: Recieve memory warning and my app crash. 例如,我可以六次访问myClass,然后获得日志:接收内存警告 ,我的应用程序崩溃。

I think myImg isn' t released from memory? 我认为myImg不是从内存中释放出来的吗? I don' t have any idea how fix it ? 我不知道如何解决? JPGs have about 100kb. JPG约有100kb。

PS. PS。 When I load .png it' s works(aslo faster) in spite of .png images have usually 600kB. 当我加载.png时,尽管.png图像通常具有600kB的速度,但它的工作原理(也更快)。 But I think it still don' t release memory ? 但是我认为它仍然不释放内存吗?

Do not use UIImage imageNamed: if you are certain that the image will not be used again within your app. 不要使用UIImage imageNamed:如果您确定该图像不会在您的应用程序中再次使用。 imageNamed: uses an internal caching mechanism that will use additional memory. imageNamed:使用内部缓存机制,该机制将使用额外的内存。

From the UIImage Class Reference : UIImage类参考

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. 如果缓存中还没有匹配的图像对象,则此方法从指定的文件加载图像数据,将其缓存,然后返回结果对象。

Instead use UIImage imageWithContentsOfFile: for single use images as those are not cached. 而是将UIImage imageWithContentsOfFile:用于一次性图像,因为这些图像不会被缓存。

Note : only imageNamed: will do automatic retina version loading (@2x). 注意 :仅imageNamed:会自动加载视网膜版本(@ 2x)。

一般而言,请尝试使用PNG图像..因为iOS已优化为非常有效地处理PNG图像..并且JPEG图像未提供此优化,所以当iOS开始显示jpg时,它需要更复杂的解码过程,这将需要花费很多时间更多的CPU能量。

将属性分配给IBOutlet并进行合成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM