Iphone 如何为动画正确预加载图像阵列
我有几个动画,当被触发时,它们在执行之前会有不同长度的(无意的)延迟 在Iphone 如何为动画正确预加载图像阵列,iphone,objective-c,ios,ipad,Iphone,Objective C,Ios,Ipad,我有几个动画,当被触发时,它们在执行之前会有不同长度的(无意的)延迟 在viewDidLoad中,我有如下内容: NSString *fileName; myArray = [[NSMutableArray alloc] init]; for(int i = 1; i < 285; i++) { fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"HD1.2 png seque
viewDidLoad
中,我有如下内容:
NSString *fileName;
myArray = [[NSMutableArray alloc] init];
for(int i = 1; i < 285; i++) {
fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"HD1.2 png sequence/HD1.2_%d", i] ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFIle:fileName];
[humptyArray addObject:image];
//NSLog(@"Added object number %d: %@", i,regularImage);
}
falling.userInteractionEnabled = NO;
falling.animationImages = humptyArray;
falling.animationDuration = 6.3f;
falling.animationRepeatCount = 1;
NSString*文件名;
myArray=[[NSMutableArray alloc]init];
对于(int i=1;i<285;i++){
文件名=[[NSBundle mainBundle]路径用于资源:[NSString stringWithFormat:@“HD1.2 png序列/HD1.2_%d”,i]类型:@“png”];
UIImage*image=[UIImage imageWithContentsOfFIle:fileName];
[humptyArray addObject:image];
//NSLog(@“添加的对象编号%d:%@”,i,图像);
}
falling.userInteractionEnabled=否;
falling.animationImages=humptyArray;
下降。动画持续时间=6.3f;
falling.animationRepeatCount=1;
我输入了NSLog
,这样我就可以确认数组中是否填充了图像。当我想触发动画时,我调用[falling startAniamting]
。虽然阵列已预加载图像,但在触发动画和执行动画之间仍存在延迟
我该怎么做才能在触发动画时不出现延迟?@autoreleasepool{
@autoreleasepool {
NSString *fileName;
humptyArray = [[NSMutableArray alloc] init];
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
for(int i = 1; i < 285; i++) {
fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"HD1.2 png sequence/HD1.2_%d", i] ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFIle:fileName];
[humptyArray addObject:image];
//NSLog(@"Added object number %d: %@", i,regularImage);
}
falling.userInteractionEnabled = NO;
falling.animationImages = humptyArray;
falling.animationDuration = 6.3f;
falling.animationRepeatCount = 1;
dispatch_async(dispatch_get_main_queue(), ^{
[humptyArray release];
});
});
}
Taken from my own answer in this other [SO question][1]
[1]: http://stackoverflow.com/a/11364856/253008
NSString*文件名;
humptyArray=[[NSMutableArray alloc]init];
dispatch\u queue\u t concurrentQueue=dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u DEFAULT,0);
调度异步(concurrentQueue,^{
对于(int i=1;i<285;i++){
文件名=[[NSBundle mainBundle]路径用于资源:[NSString stringWithFormat:@“HD1.2 png序列/HD1.2_%d”,i]类型:@“png”];
UIImage*image=[UIImage imageWithContentsOfFIle:fileName];
[humptyArray addObject:image];
//NSLog(@“添加的对象编号%d:%@”,i,图像);
}
falling.userInteractionEnabled=否;
falling.animationImages=humptyArray;
下降。动画持续时间=6.3f;
falling.animationRepeatCount=1;
dispatch\u async(dispatch\u get\u main\u queue()^{
[humptyArray release];
});
});
}
摘自我在另一个[如此问题][1]中的回答
[1]: http://stackoverflow.com/a/11364856/253008
你会发现,SO上的人建议使用animationImages,但它会占用你所有的系统内存,而且速度不快。请看一下我的答案,它是关于循环视频,但问题是相同的。请看一下我对这个问题的回答。这些链接中提供了两个示例xcode项目,显示了一个实现,它将快速开始播放并循环,不会出现任何故障—所有这些都不会占用您的所有内存并导致应用程序崩溃。在尝试此操作之前,我应该提到我正在使用ARC。除了省去发布声明之外,这会影响什么吗?我尝试了一下,但没有任何效果,使用NSTimer
比使用UIView Animation
获得更好的性能,我已经读过,这不应该是您可能没有足够的RAM的情况。如果您加载了太多的图像,UIImage将神奇地取消分配图像,并在下一次绘图时再次从磁盘读取。