Ios UIImageView动画预加载
几天来,我一直在努力寻找最有效的解决方案,将图像数组加载到.animationImages属性,并能够动态更改该数组。以下是场景: -我有一个观点 -根据用户输入(手机移动、陀螺仪),我将加载一组特定的图像来制作动画。 -在用户输入(触摸)时播放加载的动画 现在,使用initWithData从gyroHandler上运行的块(仅在某些情况下)调用“NSThread detachNewThreadSelector”,将图像数组加载到不同的线程上。其他启动方法完全失败 现在的问题是,当我第一次触摸(因为当前动画已经加载)并触发动画时,整个过程会冻结一段时间。一秒钟,然后播放动画。如果我再次触摸,它将成功播放动画,没有延迟/冻结 现在我在某个地方读到了背景动画。。。我尝试使用:Ios UIImageView动画预加载,ios,animation,uiimageview,delay,preload,Ios,Animation,Uiimageview,Delay,Preload,几天来,我一直在努力寻找最有效的解决方案,将图像数组加载到.animationImages属性,并能够动态更改该数组。以下是场景: -我有一个观点 -根据用户输入(手机移动、陀螺仪),我将加载一组特定的图像来制作动画。 -在用户输入(触摸)时播放加载的动画 现在,使用initWithData从gyroHandler上运行的块(仅在某些情况下)调用“NSThread detachNewThreadSelector”,将图像数组加载到不同的线程上。其他启动方法完全失败 现在的问题是,当我第一次触摸(
[imgAnimationKey performSelectorInBackground:@selector(startAnimating) withObject:nil];
但结果是一样的
我的阵列有19个图像,并且很可能始终具有相同的图像。
问题是我可能有更多的5+动画可以播放,这就是为什么我没有多个UIImageView
有人知道如何预加载图像并避免第一次播放时的延迟吗?或者我可以让动画在不同的线程中运行并避免这种效果(我可能做错了)
谢谢 在将图像提供给UIImageView的image属性之前,可以为UIImage类创建一个类别,以便在后台线程中预加载图像: h:
#import <Foundation/Foundation.h>
@interface UIImage (preloadedImage)
- (UIImage *) preloadedImage;
@end
#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
在后台线程上调用prepreedimage,然后在主线程上设置结果。我降低了图像质量。虽然有所改进,但仍有一些延迟。感谢您的快速响应。实际上我没有试过。我们决定不包括这个。再次感谢。如果是,请发布您的答案,以便任何人都能从中受益:-)