简体   繁体   中英

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

I have problem with memory. I use ARC. I' m loading .jpgs to UIImageView on my view.

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. I can come myClass for example six times then I have log: Recieve memory warning and my app crash.

I think myImg isn' t released from memory? I don' t have any idea how fix it ? JPGs have about 100kb.

PS. When I load .png it' s works(aslo faster) in spite of .png images have usually 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. imageNamed: uses an internal caching mechanism that will use additional memory.

From the 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.

Instead use UIImage imageWithContentsOfFile: for single use images as those are not cached.

Note : only imageNamed: will do automatic retina version loading (@2x).

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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