简体   繁体   English

UIImageView动画预加载

[英]UIImageView animation preload

I've been battling for a few days trying to get the most effective/efficient solution to load an array of images to the .animationImages properties and be able to change that array on the fly. 我努力了几天,试图获得最有效/高效的解决方案,以将图像数组加载到.animationImages属性,并能够即时更改该数组。 Here is the scenario: - I have one UIImageView - Depending on user input (phone movement, gyro) I will load a specific array of images to animate. 这是场景:-我有一个UIImageView-根据用户输入(电话移动,陀螺仪),我将加载特定的图像阵列进行动画处理。 - On user input (touch) play the animation loaded. -在用户输入(触摸)时播放加载的动画。

Now, the array of images is loaded on a different thread using "NSThread detachNewThreadSelector" called from the block ran on the gyroHandler (only on certain cases) using the initWithData. 现在,使用“ NSThread detachNewThreadSelector”将图像数组加载到不同的线程上,该调用是使用initWithData在gyroHandler上运行的块调用的(仅在某些情况下)。 Other initiation methods were a complete fail. 其他启动方法完全失败。

Now the issue is that when I touch for the first time (since the current animation has been loaded) and triggers the animation the whole thing freezes for aprox. 现在的问题是,当我第一次触摸(因为当前的动画已加载)并触发动画时,整个动作都冻结为aprox。 one second and then plays the animation. 一秒钟,然后播放动画。 If I touch again it plays the animation successfully with no delay/freeze. 如果再次触摸,它将成功播放动画,没有延迟/冻结。

Now i read somewhere to animate in the background... I tried using: 现在,我在某处阅读背景动画……我尝试使用:

[imgAnimationKey performSelectorInBackground:@selector(startAnimating) withObject:nil];

but the outcome was the same. 但是结果是一样的。

My array has 19 images and most likely will have the same always. 我的阵列有19张图像,最有可能总是保持相同。 Problem is I might have more 5+ animations that could play that is why I dont have multiple UIImageViews. 问题是我可能有5种以上的动画可以播放,这就是为什么我没有多个UIImageViews的原因。

Anybody knows a way to preload the images and avoid the delay on first play? 有人知道一种预加载图像并避免第一次播放延迟的方法吗? Or can I make the animation to run in a different thread and avoid this effect (I might be doing it wrong)? 还是可以使动画在其他线程中运行并避免这种效果(我可能做错了)?

Thanks! 谢谢!

You could make a category for the UIImage class for preloading your image in a background thread before you provide it to the image property of your UIImageView: 您可以为UIImage类创建一个类别,以便在将其提供给UIImageView的image属性之前在后台线程中预加载图像:

h: H:

#import <Foundation/Foundation.h>

@interface UIImage (preloadedImage)

- (UIImage *) preloadedImage;

@end

m: m:

#import "UIImage+preloadedImage.h"

@implementation UIImage (preloadedImage)

- (UIImage *) preloadedImage {
    CGImageRef image = self.CGImage;

    // make a bitmap context of a suitable size to draw to, forcing decode
    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);

    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef imageContext =  CGBitmapContextCreate(NULL, width, height, 8, width * 4, colourSpace,
                                                       kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
    CGColorSpaceRelease(colourSpace);

    // draw the image to the context, release it
    CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image);

    // now get an image ref from the context
    CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);

    UIImage *cachedImage = [UIImage imageWithCGImage:outputImage];

    // clean up
    CGImageRelease(outputImage);
    CGContextRelease(imageContext);

    return cachedImage;
}

@end

make the call of the preloadedImage on a background thread then and set the result on the main thread. 在后台线程上调用preloadedImage,然后在主线程上设置结果。

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

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