iOS:提高图像绘制速度 [英] iOS: Improving speed of image drawing

查看:102
本文介绍了iOS:提高图像绘制速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列要设置动画的图像( UIImageView 支持一些基本动画,但不足以满足我的需要)。

I have a sequence of images that I want to animate (UIImageView supports some basic animation but it's not sufficient for my needs).

我的第一种方法是使用 UIImageView 并在设置图像时设置 image 属性。这太慢了。速度较差的原因是由于绘制了图像(这让我感到惊讶;我认为瓶颈会加载图像)。

My first approach was to use UIImageView and set the image property when the image. This was too slow. The reason for the poor speed was due to the drawing of the images (which surprised me; I assumed the bottle neck would be loading the image).

我的第二种方法是使用通用的 UIView 并设置 view.layer.contents = image.CGImage

My second approach was to use a generic UIView and set view.layer.contents = image.CGImage. This gave no noticeable improvement.

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

Both of these approaches must be performed on the main thread. I think the poor speed is due to having to draw the image data to a CGContext.

如何提高绘图速度?

推荐答案

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

I managed to improve performance by doing a few things:


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

  • I fixed my build process so that the PNGs were being iOS optimized. (The content for the app is being managed in a separate project which outputs a bundle. The default bundle settings are for an OS X bundle, which does not optimize PNGs).

在后台线程I:


  1. 创建新的位图上下文(以下代码)

  2. 将PNG图像绘制到位图上下文中

  3. 从位图上下文中创建CGImageRef

  4. 设置 layer.content 在CGImageRef的主线程上

  1. Created a new bitmap context (code below)
  2. Drew the PNG image into the bitmap context
  3. Created a CGImageRef from the bitmap context
  4. Set layer.content on the main thread to the CGImageRef


  • 使用了 NSOperationQueue 来管理操作。

    我敢肯定有更好的方法,但是

    I'm sure there's a better way of doing this, but the above results in acceptable performance.

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

    这篇关于iOS:提高图像绘制速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆