Objective c 在UIImageView中加载多个160帧的动画
我正在尝试加载4 UIImageView以运行不同的动画。每个动画由164个大小为6KB的png组成 我已经在仪器中进行了测试,在模拟器中运行时效果良好,内存大小小于5,5MB 但当在iPad上运行时,应用程序只是在没有日志信息或内存警告的情况下崩溃。我在仪器中测试过,崩溃前内存大小小于5MB 这是代码Objective c 在UIImageView中加载多个160帧的动画,objective-c,animation,uiimageview,Objective C,Animation,Uiimageview,我正在尝试加载4 UIImageView以运行不同的动画。每个动画由164个大小为6KB的png组成 我已经在仪器中进行了测试,在模拟器中运行时效果良好,内存大小小于5,5MB 但当在iPad上运行时,应用程序只是在没有日志信息或内存警告的情况下崩溃。我在仪器中测试过,崩溃前内存大小小于5MB 这是代码 -(void)startAnimations { OTAnimationProvider *ani = [[OTAnimationProvider alloc] init];
-(void)startAnimations
{
OTAnimationProvider *ani = [[OTAnimationProvider alloc] init];
int a;
for (a=0;a<[teams CountY];a++)
{
UIImageView *animationTeam = [[UIImageView alloc] initWithFrame:CGRectMake(10, 300 + (40 * a), 1004, 300)];
int idTeam = [[teams getValueByName:@"IdTeam" posY:a] intValue];
[animationTeam setAnimationImages:[ani animationWithName:[NSString stringWithFormat: @"OTScoreBoardLeague_%d_%d_",idTeam,a+1] frameCount:164 step:2]];
[animationTeam setAnimationDuration:animationPlayersTime];
[animationTeam setAnimationRepeatCount:1];
[animationTeam startAnimating];
[self.view addSubview:animationTeam];
[animationTeam release];
}
[ani release];
}
有什么想法吗
谢谢。PNG的尺寸是多少,以像素为单位?这些图像有很多透明区域吗?像素为1004 x 300。是的,确实有许多透明区域。减少图像数量是否最终能防止崩溃?您是否尝试过添加简单打印以检测应用程序停止运行的位置?在调试器(附加到Xcode/gdb)中运行代码时会发生什么情况?与没有透明度的相同图像相比,在为具有透明度的图像设置动画时,iOS设备可能会消耗大量资源。这将是我尝试的第一件事。
CALayer *layer = [CALayer layer];
CGImageRef img = [[UIImage imageNamed:@"OTScoreBoardLeagueAtlasTeam_1_1.png"]CGImage];
layer.contents = (id)img;
CGSize size = CGSizeMake( 1004, 300 ); // size in pixels of one frame
CGSize normalizedSize = CGSizeMake( size.width/CGImageGetWidth(img), size.height/CGImageGetHeight(img) );
layer.bounds = CGRectMake( 0, 0, size.width, size.height );
layer.contentsRect = CGRectMake( 10, 300 + (20 * a), normalizedSize.width, normalizedSize.height );
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"sampleIndex"];
anim.fromValue = [NSNumber numberWithInt:1]; // initial frame
anim.toValue = [NSNumber numberWithInt:165]; // last frame + 1
anim.duration = 8.0f; // from the first frame to the 6th one in 1 second
anim.repeatCount = 0; // just keep repeating it
anim.autoreverses = NO; // do 1, 2, 3, 4, 5, 4, 3, 2
[layer addAnimation:anim forKey:nil]; // start
[self.view.layer addSublayer:layer];