Dan*_*pie 1 iphone objective-c uiimageview ipad ios
我有一个21帧的菜单背景动画.我使用视图的viewDidLoad方法中的以下代码将它们加载到内存中.
NSMutableArray *menuanimationImages = [[NSMutableArray alloc] init];
for( int aniCount = 0; aniCount < 21; aniCount++ )
{
NSString *fileLocation = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: @"bg%i", aniCount + 1] ofType: @"png"];
NSData *imageData = [NSData dataWithContentsOfFile: fileLocation];
[menuanimationImages addObject: [UIImage imageWithData:imageData]];
}
settingsBackground.animationImages = menuanimationImages;
Run Code Online (Sandbox Code Playgroud)
不幸的是,做[settingsBackground startAnimating]; 在viewDidLoad方法中不起作用.有没有办法预加载动画,所以第一次运行没有1-3秒的延迟?
我通常不建议使用imageNamed并依赖于内置的缓存机制.如果你搜索,你会发现很多关于这个问题的讨论,但它也不一定会预渲染你的图像.
我使用以下代码预加载和预渲染图像,以便在第一次动画时没有延迟.
NSMutableArray *menuanimationImages = [[NSMutableArray alloc] init];
for (int aniCount = 1; aniCount < 21; aniCount++) {
NSString *fileLocation = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: @"bg%i", aniCount + 1] ofType: @"png"];
// here is the code to pre-render the image
UIImage *frameImage = [UIImage imageWithContentsOfFile: fileLocation];
UIGraphicsBeginImageContext(frameImage.size);
CGRect rect = CGRectMake(0, 0, frameImage.size.width, frameImage.size.height);
[frameImage drawInRect:rect];
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[menuanimationImages addObject:renderedImage];
}
settingsBackground.animationImages = menuanimationImages;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3119 次 |
| 最近记录: |