我有一系列想要制作动画的图像(UIImageView
支持一些基本的动画,但不足以满足我的需求)。
我的第一个方法是使用UIImageView
并设置image
当图像属性。这太慢了。速度慢的原因是图像的绘制(这让我感到惊讶;我以为瓶颈会加载图像)。
我的第二种方法是使用通用的UIView
并设置view.layer.contents = image.CGImage
。这没有带来明显的改善。
这两种方法都必须在主线程上执行。我认为速度慢是因为必须将图像数据绘制到CGContext
.
如何提高绘图速度?是否可以在后台线程上绘制上下文?
我通过做一些事情来提高性能:
我确信有更好的方法可以做到这一点,但上述方法会产生可接受的性能。
-(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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)