Hi
I've read that imageNamed: is bad when trying to initialize images. But then what is the best way? I am using imageWithContentsOfFile: and passing the path of an image in my resources folder
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"]
This call is made some 30 times in a for loop.
Now when I run my app with instruments, I see that a lot of memory is used up by NSString for operations like the above one where we use string literals (@"jpg") Instruments shows the responsible caller as [NSBundle mainBundle] and this in turn points to the line when I use the string literal for the type.
So what is the most effective way of initializing images without using too much of memory?
I changed the statement to
img = [UIImage imageWithContentsOfFile:[bndl pathForResource:fileName ofType:extn]]
where extn
is static and initialized to @"jpg"
. fileName
keeps changing for each iteration of the for loop. But even then the maximum use of NSString
is because of [NSBundle mainBundle]
and [NSBundle pathForResource:OfType:]
according to Instruments.