Jul 27
In my last project, I used more than 100 images. It’s OK while I was testing it on the simulator. But when I uploaded it onto the device, it crashed all the time. I check the crash log, it showed as “low memory”.

I used this to load image:

 Objective-C |  copy code |? 
1
singleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.jpg"]];

When I run it with Instruments, I noticed that the use of the memory were increasing very fast and seemed to be not released. That's why the app crashed and through out a 101 error. It looks like that the imageNamed will cache images in the memory and only release the memory while the app terminate. Then I use imageWithContentsOfFile for a replacement of imageNamed:

 Objective-C |  copy code |? 
1
NSString* str = [[NSBundle mainBundle] pathForResource:@"test.jpg" ofType:nil inDirectory:@""];
2
singleImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:str]];
3

The problem solved! You can see the snapshot of Instruments below, the memory were released when I changed to another category.



The imageWithContentsOfFile might be slower than imageNamed, but it is worthy to use it.