iOS:提高图像绘制的速度

我有一个图像序列,我想animation( UIImageView支持一些基本的animation,但它不足以满足我的需要)。

我的第一个方法是使用UIImageView并设置image属性时,图像。 这太慢了。 速度不佳的原因是由于图像的绘制(这令我感到惊讶,我认为瓶颈将会加载图像)。

我的第二种方法是使用通用的UIView并设置view.layer.contents = image.CGImage 。 这并没有明显的改善。

这两种方法都必须在主线程上执行。 我认为速度不佳是由于不得不将图像数据绘制到CGContext

我怎样才能提高绘图速度? 是否有可能在后台线程上的上下文?

我设法通过做一些事情来提高性能:

  • 我修复了我的构build过程,以便对PNG进行iOS优化。 (应用程序的内容在一个单独的项目中进行pipe理,该项目会输出一个包。默认的包设置是针对OS X包的,它不会优化PNG)。

  • 在后台线程上我:

    1. 创build了一个新的位图上下文(代码如下)
    2. 将PNG图像绘制到位图上下文中
    3. 从位图上下文创build一个CGImageRef
    4. 将主线程上的layer.content设置为CGImageRef
  • 使用NSOperationQueue来pipe理操作。

我相信有这样做的更好的方法,但上述结果在可接受的性能。

 -(CGImageRef)newCGImageRenderedInBitmapContext //this is a category on UIImage { //bitmap context properties CGSize size = self.size; NSUInteger bytesPerPixel = 4; NSUInteger bytesPerRow = bytesPerPixel * size.width; NSUInteger bitsPerComponent = 8; //create bitmap context unsigned char *rawData = malloc(size.height * size.width * 4); memset(rawData, 0, size.height * size.width * 4); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(rawData, size.width, size.height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); //draw image into bitmap context CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), self.CGImage); CGImageRef renderedImage = CGBitmapContextCreateImage(context); //tidy up CGColorSpaceRelease(colorSpace); CGContextRelease(context); free(rawData); //done! //Note that we're not returning an autoreleased ref and that the method name reflects this by using 'new' as a prefix return renderedImage; } 

什么是你的animation需求,使用UIImage *(animationImages)和伴随的animationDuration和animationRepeatCount不工作使用UIImageView的内置animation服务?

如果您正在快速绘制多个图像,请仔细查看Quartz 2D。 如果你正在绘画,然后animation(移动,缩放等)的图像,你应该看看核心animation。

这听起来像Quartz 2D是你想要的。 苹果文档在这里: http : //developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html