Probably I'm gonna ask the same question which was asked by other person (But it has no responses):
Speed up first UIImageView animation (force cache the images)
But, My short question is:
I have 60 images in resources, in timeinterval loop I'm gonna animate that images, each time setting to UIImageView.image the n'th image from resouce.
The problem is: first animation is bad! When I loop all images again in same UIImageView, animation is perfect. Can we pre cache images in UIImageView?
EDIT: Or maybe we can make some tricks, to make animation smooth?

- 1
- 1

- 4,371
- 10
- 58
- 97
3 Answers
The method
[UIImage imageNamed:@""]
caches the image. The UIImageView doesn't. I had a problem in an app with a lot of images that was crashing due to low memory.
To fix if I changed to [UIImage imageWithContentsOfFile:@""]
that does not caches the image.
To pre-cache the image you can simply call [UIImage imageNamed:@""]
for all images you want in the init method. But if you receive a memory warning the images gonna be deallocated.

- 1,017
- 9
- 12
-
Maybe, but we talking about UIImageView, not UIImage – beryllium Nov 25 '11 at 17:07
-
YES! During profiling app with using `imageNamed`, my app took 120mb memory, when used `imageWithContentsOfFile` memory is 15mb. But unfortunately it doesn't solve bad animation problem( – Almas Adilbek Nov 25 '11 at 17:42
-
Do you tried to instantiate all the images before the first animation call with [UIImage imageNamed:@""]? – Bruno Domingues Nov 25 '11 at 17:46
-
No! I think that will cause memory warning – Almas Adilbek Nov 25 '11 at 18:51
-
Ok, let's think I've initialized images with [UIImage imageNamed:@""], then how to release them from cache? – Almas Adilbek Nov 25 '11 at 19:04
-
unfortunately I think it's not possible to clear imageNamed cache. It'll be released only when your app receives a memory warning. Maybe it's better to use imageWithContentsOfFile and save the images in an array in your init method, then you release the array when you don't need the images anymore. – Bruno Domingues Nov 26 '11 at 15:42
-
Thank you, I will use `imageWithContentsOfFile` and store images in array. Should I iterate array to release all images? Or, releasing array itself is enough? – Almas Adilbek Nov 27 '11 at 05:06
-
Releasing the array is enough, the array retain all the images and when you release it, it releases all it's children. – Bruno Domingues Nov 27 '11 at 15:38
-
1@BrunoDomingues you said that with **imageNamed**, the app is crashed due to low memory, but then you said images in cache are deallocated when your app receives memory warning ?? – onmyway133 Oct 28 '13 at 10:57
UIImageView does not cache the image, as [UIImage imageNamed:@""]
does. Unfortunately, if you have a lot of images, imageNamed will crash your application because it runs out of memory.
You can pre-load all of the images in an NSArray using [UIImage imageWithContensOfFile:@""]
.
Once you have the array, you can do what you want!

- 44,465
- 11
- 90
- 113

- 1,374
- 3
- 13
- 33
-
1I think it is strange!.. NSArray will also keep memory of images inside itself, as well as images cached by imageNamed. Or? – Almas Adilbek Nov 25 '11 at 17:38
No, UIImageView does not cache images. image
property declared as retain, so when you set new image, then imageView send release
message to old image and retain
to new one.
The easiest way to animate UIImageView is to set array of images to property animationImages
and call startAnimating method
imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.png"], ..., nil];
imageView.animationDuration = 2.0f;
[imageView startAnimating];
-
But, have you ever tried to animate 100 images, each with 850x535 size using UIImageView.animationImages? I've tried it, app crashed because of memory.. The reason I think is, because animationImages holds all images, which are a lot of memory. OR, am I saying something wrong?? If so, please correct me. Thx – Almas Adilbek Nov 25 '11 at 16:37
-
Yes, i guess you exactly right. This way is easiest, but not efficient as you can see. – beryllium Nov 25 '11 at 16:43
-
What you think, should I use some graphics library to play with big images, keeping performance good? Do you know any? Thx – Almas Adilbek Nov 25 '11 at 16:48
-
@AlmasAdilbek, You can do it with default UIKit classes. If your first way get weird results, then try this. Show in current time only 1 imageView, when you want to show next image, then remove imageView from superView and create new with new image – beryllium Nov 25 '11 at 16:55
-
The imageNamed method does cache the images in memory. A working solution that handles many images can be found at this SO answer: https://stackoverflow.com/questions/442076/method-for-animating-images-like-a-movie-on-iphone-without-using-mpmovieplayer#answer-6077394 – MoDJ Mar 20 '15 at 20:46