在iphone上使用objective c预加载UIImageView动画



我有一个很好的动画图像。它由180张高质量的图像组成,播放精细,连续循环。我的问题是,我第一次加载包含这些图像的视图需要很长时间才能加载。之后的每次后续时间,它立即加载,因为我假设图像已被缓存或预加载!!我来自flash背景,我相信你知道预加载器是很常见的,所以我不觉得这应该很难找到,但经过无数次的谷歌搜索,我找不到任何关于预加载的好例子,也找不到任何关于为什么会有延迟以及如何做的文章。

所以我的问题是:

  1. 是否有一个复选框在信息。在应用程序开始时预加载我所有的图像?

  2. 如何预加载图像,是否有任何简单的示例项目,我可以看看?

  3. 这是实现本质上是视频但已输出到png序列的最佳方式吗?

  4. 是否有另一个方法viewDidLoad不工作,因为我期望它做。它跟踪"FINISHED LOADING IMAGES"(见下面的代码),但视图在图片加载后不会显示一到两秒钟,所以如果视图在图片加载后才显示,那么UIActivityIndicatorView也会在同一个视图中显示。

  5. 如何在objective c中进行事件侦听?

下面是viewDidLoad中的代码,我认为这是相当标准的:

任何帮助都是非常感激的,因为我在ui开发中似乎如此基本的东西上撞了我的头。帮助:)

- (void)viewDidLoad {
    [super viewDidLoad];
    imageArray = [[NSMutableArray alloc] initWithCapacity:IMAGE_COUNT];
    NSLog(@"START LOADING IMAGES");
    // Build array of images, cycling through image names
    for (int i = 0; i < IMAGE_COUNT; i++){
        [imageArray addObject:[UIImage imageNamed: [NSString stringWithFormat:@"Main_%d.png", i]]];
    }
    animatedImages = [[UIImageView alloc] initWithFrame:CGRectMake(0,20,IMAGE_WIDTH, IMAGE_HEIGHT)];
    animatedImages.animationImages = [NSArray arrayWithArray:imageArray];
    animatedImages.animationDuration = 6.0;
    animatedImages.animationRepeatCount = 0;    
    [self.view addSubview:animatedImages];
    animatedImages.startAnimating;
    [animatedImages release];
    NSLog(@"FINISH LOADING IMAGES");
} 

欢呼

如果有人发现这个问题,我有一个答案,那就是像这样预渲染图像。

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 load and 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();
    // then add the resulting image to the array
    [menuanimationImages addObject:renderedImage];
}
settingsBackground.animationImages = menuanimationImages;

我已经尝试了多种其他预加载图像的方法,这是我发现唯一有效的方法。

My problem is that the first time I load the view containing these images it takes a long time to load. Every subsequent time after that it loads immediately as I am assuming that the images have been cached or preloaded

你在这一点上是正确的......当您使用方法imageNamed:时,此方法文档引用.....

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

所以在我看来,与其在viewDidLoad中执行以下操作,不如在延迟不大的情况下更早执行......

    for (int i = 0; i < IMAGE_COUNT; i++)
    { 
    [imageArray addObject:[UIImage imageNamed: [NSString stringWithFormat:@"Main_%d.png", i]]]; 
}

另一种方法

- (void)spinLayer:(CALayer *)inLayer duration:(CFTimeInterval)inDuration
        direction:(int)direction
{
    CABasicAnimation* rotationAnimation;
    // Rotate about the z axis
    rotationAnimation = 
    [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    // Rotate 360 degress, in direction specified
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 * direction];
    // Perform the rotation over this many seconds
    rotationAnimation.duration = inDuration;
    rotationAnimation.repeatCount = 100;
    //rotationAnimation.
    // Set the pacing of the animation
    //rotationAnimation.timingFunction = 
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    // Add animation to the layer and make it so
    [inLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}

这个方法将有助于在动画中调用它如下(我假设你把上面的方法在同一个类中,你有imageView

[self spinLayer:yourImageView.layer duration:5.0
            direction:<-1 or 1 for anti clockwise or clockwise spin>];

记住,只设置一个图像的imageView(你希望动画。谢谢你,

最新更新