我在tableView
的{{1}}方法中调用了以下方法。
cellForRowAtIndexPath:
- (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:
我必须在标头中声明{
...
equalizer = (UIImageView *) [cell viewWithTag:30];
equalizer.hidden = YES;
if (entry.audioManager && [entry.audioManager soundPlayer].isPlaying)
{
equalizer.hidden = NO;
[self animateTheEqualizer];
}
return cell;
}
,因为我在两种方法中都访问它。但是对于所有名为equalizer
的{{1}},我不确定哪个地方是最好的声明地点,因为它们只用于那个方法,但每次调用方法时都会声明它们UIImage
似乎没有内存效率,而我不确定是否公开在标题中声明它们是更好的选择。
哪个更好,效率更高?
我最终如何解决这个问题:
感谢所有回答的人,我使用@Antonio和@ x4h1d的答案组合如下:
frame1, frame2, ..., frame10
然后cellForRowAtIndexPath:
方法:
@implementation MyClass
static NSMutableArray *imageArray;
static UIImage *frame;
然后在+ (void) initialize
的{{1}}方法中使用此位:
+ (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;
}
}
答案 0 :(得分:1)
如果图像没有改变,最好的方法是将它们声明为静态
@implementation MyClass
static UIImage *frame1, *frame2, *frame3, *frame4, *frame5, *frame6, *frame7, *frame8, *frame9, *frame10;
+ (void) initialize {
frame1 = [UIImage imageNamed:@"equalizer1"];
frame2 = [UIImage imageNamed:@"equalizer2"];
frame3 = [UIImage imageNamed:@"equalizer3"];
frame4 = [UIImage imageNamed:@"equalizer4"];
frame5 = [UIImage imageNamed:@"equalizer5"];
frame6 = [UIImage imageNamed:@"equalizer6"];
frame7 = [UIImage imageNamed:@"equalizer7"];
frame8 = [UIImage imageNamed:@"equalizer8"];
frame9 = [UIImage imageNamed:@"equalizer9"];
frame10 = [UIImage imageNamed:@"equalizer10"];
}
initialize
方法是一个静态构造函数,它是在第一次使用该类时调用的。使用静态变量的好处是它们被实例化一次,具有关于速度(一次执行操作)和内存(一次实例化图像)的固有优势。
另一种方法是静态实例化数组:
@implementation MyClass
static NSArray *_animationImages;
+ (void) initialize {
_animationImages = @[
[UIImage imageNamed:@"equalizer1"],
[UIImage imageNamed:@"equalizer2"],
[UIImage imageNamed:@"equalizer3"],
[UIImage imageNamed:@"equalizer4"],
[UIImage imageNamed:@"equalizer5"],
[UIImage imageNamed:@"equalizer6"],
[UIImage imageNamed:@"equalizer7"],
[UIImage imageNamed:@"equalizer8"],
[UIImage imageNamed:@"equalizer9"],
[UIImage imageNamed:@"equalizer10"]
];
}
然后在你的方法中:
- (void)animateTheEqualizer {
equalizer.animationImages = _animationImages;
[equalizer startAnimating];
}
后者的解决方案在我看来更好,但当然如果_animationImages不需要更新(例如更改图像或其顺序),它就可以使用。
答案 1 :(得分:1)
如果表视图的所有实例都可以共享相同的帧变量,那么您可以更改现有的声明,如下所示。静态声明只会被执行一次,并且变量将随后可用 - 仅在该方法中 - 随后随后调用该方法,包括表视图的任何其他实例。
- (void)animateTheEqualizer
{
static UIImage *frame1 = [UIImage imageNamed:@"equalizer1"];
static UIImage *frame2 = [UIImage imageNamed:@"equalizer2"];
static UIImage *frame3 = [UIImage imageNamed:@"equalizer3"];
static UIImage *frame4 = [UIImage imageNamed:@"equalizer4"];
static UIImage *frame5 = [UIImage imageNamed:@"equalizer5"];
static UIImage *frame6 = [UIImage imageNamed:@"equalizer6"];
static UIImage *frame7 = [UIImage imageNamed:@"equalizer7"];
static UIImage *frame8 = [UIImage imageNamed:@"equalizer8"];
static UIImage *frame9 = [UIImage imageNamed:@"equalizer9"];
static 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];
}
答案 2 :(得分:1)
delcare a iVar或私人NSMutableArray
,说 imageArray 。在ViewDidLoad
方法中插入图片如下,
imageArray = [[NSMutableArray alloc] init];
for(NSUInteger i = 1; i <= 10; i++)
{
UIImage *frame = [UIImage imageNamed:[NSString stringWithFormat:@"equalizer%i",i]];
if(frame){
[imageArray addObject:frame];
}else {
// handle if image is not there
}
frame = nil;
}
equalizer.animationImages = [imageArray copy];
[equalizer startAnimating];
如果图像是静态的而不是在课外使用,我认为这将是有效的方法。顺便说一句,如果您认为不再需要它,请将 imageArray 设置为nil。
答案 3 :(得分:0)
将它们作为私有变量放在这里:
@implementation viewController
{
UIImage *frame1;
...
}
然后将所有这些放在viewDidLoad
:
frame1 = [UIImage imageNamed:@"equalizer1"];
如果不改变函数,你不应该在每次调用函数时声明它们或启动它们
答案 4 :(得分:0)
如果要在其他类中公开此属性,则应在.h文件中声明均衡器。如果你需要它只在这个类中声明.m文件中的私有。
如果你没有充分的理由这样做,你不必宣布10个UIImages(属性)。 在您的示例中,您在animateTheEqualizer方法中创建10个本地UIImage变量,它工作正常。如果您需要在任何其他地方访问它,您可以致电:
UIImage *frame1 = equalizer.animationImages[0];
EXTENDED:
你可以只加载一次数组,比如说在viewDidLoad:
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];
你只使用UIImage的局部变量。
之后当您想要调用动画时,请致电:
[equalizer startAnimating];