I have the following method that is called in my tableView
's cellForRowAtIndexPath:
method.
- (void)animateTheEqualizer
{
UIImage *frame1 = [UIImage imageNamed:@"equalizer1"];
UIImage *frame2 = [UIImage imageNamed:@"equalizer2"];
UIImage *frame3 = [UIImage imageNamed:@"equalizer3"];
UIImage *frame4 = [UIImage imageNamed:@"equalizer4"];
UIImage *frame5 = [UIImage imageNamed:@"equalizer5"];
UIImage *frame6 = [UIImage imageNamed:@"equalizer6"];
UIImage *frame7 = [UIImage imageNamed:@"equalizer7"];
UIImage *frame8 = [UIImage imageNamed:@"equalizer8"];
UIImage *frame9 = [UIImage imageNamed:@"equalizer9"];
UIImage *frame10 = [UIImage imageNamed:@"equalizer10"];
// UIImageView *equalizer;
equalizer.animationImages = [[NSArray alloc] initWithObjects:frame1, frame2, frame3, frame4, frame5, frame6, frame7, frame8, frame9, frame10, nil];
[equalizer startAnimating];
}
cellForRowAtIndexPath:
method:
{
...
equalizer = (UIImageView *) [cell viewWithTag:30];
equalizer.hidden = YES;
if (entry.audioManager && [entry.audioManager soundPlayer].isPlaying)
{
equalizer.hidden = NO;
[self animateTheEqualizer];
}
return cell;
}
I had to declare equalizer
in the header, because I access it in both methods. However for all the UIImage
s named frame1, frame2, ..., frame10
I'm not sure where is the best place to be declared, since they're only used in that method, but declaring them every time the method is called in cellForRowAtIndexPath:
seems not to be memory efficient, while I'm not sure if declaring them in the header publicly, is a better choice.
Which is better and more efficient?
How I ended up really solving this:
Thanks to all those who answered, I used a combination of @Antonio and @x4h1d 's answers as follows:
@implementation MyClass
static NSMutableArray *imageArray;
static UIImage *frame;
And then + (void) initialize
method:
+ (void) initialize
{
imageArray = [[NSMutableArray alloc] init];
for(NSUInteger i = 1; i <= 10; i++)
{
UIImage *frame = [UIImage imageNamed:[NSString stringWithFormat:@"equalizer%lu",(unsigned long)i]];
if(frame)
{
[imageArray addObject:frame];
}
else
{
// handle if image is not there
}
frame = nil;
}
}
and then this bit in my cellForRowAtIndexPath:
method for the tableView
:
equalizer = (UIImageView *) [cell viewWithTag:30];
equalizer.hidden = YES;
if (entry.audioManager && [entry.audioManager soundPlayer].isPlaying)
{
equalizer.hidden = NO;
[self animateTheEqualizer];
}