使用i的目标c预加载UIImageView动画

时间:2011-04-12 07:50:04

标签: iphone objective-c ipad

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

所以我的问题是:

  1. info.plist中是否有一个复选框可以在应用开始时预加载我的所有图片?

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

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

  4. 是否有另一种方法,因为viewDidLoad不能像我期望的那样工作。它跟踪“完成的加载图像”(参见下面的代码),但视图在图像加载后没有显示一两秒,所以如果视图在图像加载之前没有显示,那么UIActivityIndi​​catorView也不会相同的观点。

  5. 您如何在目标c中进行事件聆听?

  6. 以下是我认为相当标准的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");
    
    } 
    

    干杯

    中号

2 个答案:

答案 0 :(得分:7)

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

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;

我已尝试过多种其他预加载图像的方法,这是我发现的唯一可行的方法。

答案 1 :(得分:0)

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(您希望设置动画。 感谢,