I read that imageNamed: is bad when trying to initialize images. But then the best way? I am using imageWithContentsOfFile: and passing the image path in the resource folder
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"]
This call is made approximately 30 times in a for loop.
Now, when I launch my application using tools, I see that most of the memory is used by NSString for operations similar to those described above, where we use string literals (@ "jpg") The tools show the responsible caller as [NSBundle mainBundle], and This in turn points to a string when I use a string literal for a type.
So what is the most efficient way to initialize images without using too much memory?
I changed the instruction to
img = [UIImage imageWithContentsOfFile:[bndl pathForResource:fileName ofType:extn]]
where extn static and initialized to @"jpg" . fileName continues to change for each iteration of the for loop. But even then, the maximum use of NSString is due to [NSBundle mainBundle] and [NSBundle pathForResource:OfType:] according to the tools.
source share