简体   繁体   English

使用目标C为iPhone预加载UIImageView动画

[英]Preloading a UIImageView animation using objective c for the iphone

I have an animated image which works great. 我有一个动画效果很好的图像。 It consists of 180 high quality images and it plays fine and loops continuously. 它包含180张高质量图像,并且可以连续播放并循环播放。 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!!! 此后每隔一段时间,它会立即加载,因为我假设图像已缓存或预加载!!! I come from a flash background and as I am sure you aware preloaders are as common as muck so I don't feel this should be difficult to find but after countless googling I cannot find any good examples on preloading or any articles on why there is a delay and what to do about it. 我来自Flash背景,可以肯定的是,您知道预加载器和渣土一样普遍,因此我不觉得很难找到这种预加载器,但是经过无数次谷歌搜索之后,我找不到任何有关预加载的好的示例或为何存在预加载器的任何文章。延迟及其处理方法。

So my question(s) is this: 所以我的问题是这样的:

  1. Is there a checkbox in the info.plist to preload all my images at the start of the app? info.plist中是否有一个复选框可以在应用程序启动时预加载我的所有图像?

  2. How can you preload images and are there any simple example projects that I could look at? 您如何预加载图像?有没有我可以看的简单示例项目?

  3. Is this the best way to implement what is essentially a video but has been output to a png sequence? 这是实现本质上是视频但已输出到png序列的最佳方法吗?

  4. Is there another method as viewDidLoad does not work as I expect it to do. 还有另一种方法,因为viewDidLoad无法正常工作。 It traces "FINISHED LOADING IMAGES" (see code below) but the view does not show for a second or two after the images have been loaded so if the view does not show until the images have loaded then neither will the UIActivityIndicatorView which is also in the same view. 它会跟踪“完成的加载图像”(请参见下面的代码),但是在加载图像后视图不会显示一两秒钟,因此,如果在加载图像后才显示视图,则UIActivityIndi​​catorView也不会显示在视图中相同的看法。

  5. How do you do event listening in objective c? 您如何在目标c中进行事件监听?

Below is the code in the viewDidLoad which I believe is fairly standard: 下面是viewDidLoad中的代码,我认为这是相当标准的:

Any help is greatly appreciated as I am banging my head on a brick wall on something that seems so basic in ui development. 我在ui开发中看起来很基础的东西上砸了砖墙,对我的任何帮助都深表感谢。 Help :) 救命 :)

- (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");

} 

Cheers 干杯

M 中号

In case someone finds this question, I have an answer, which is to pre-render the images like this. 万一有人发现这个问题,我有一个答案,那就是像这样预渲染图像。

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;

I have tried multiple other methods of pre-loading images, and this is the only thing I've found that works. 我尝试了其他多种预加载图像的方法,这是我发现唯一可行的方法。

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

you are right at this point ...... as you are using method imageNamed : for this method document quotes..... 您在这一点上是正确的.........您正在使用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.

so in my opinion, rather than doing following stuff in viewDidLoad, you should do it earlier where delay is of not considerable...... 因此,我认为,与其在viewDidLoad中进行后续工作,不如在延迟不大的情况下,应更早地进行操作……

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

another approach 另一种方法

- (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"];
}

this method will help in animation call it as follow(I am assuming that you are putting above method in same class where you have imageView . 此方法将有助于动画按如下方式进行调用(我假设您将上述方法放在具有imageView同一类中。

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

remember just set only one image to that imageView (which you wish to animate. thanks, 请记住,仅将一个图像设置为该imageView (您希望对其进行动画处理。谢谢,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM