Ben*_*hen 5 calayer quartz-graphics cgcontext ios
我有一系列想要制作动画的图像(UIImageView支持一些基本的动画,但它不足以满足我的需要).
我的第一种方法是在图像时使用UIImageView和设置image属性.这太慢了.速度差的原因是由于图像的绘制(这使我感到惊讶;我认为瓶颈会加载图像).
我的第二种方法是使用泛型UIView和集合view.layer.contents = image.CGImage.这没有明显的改善.
这两种方法都必须在主线程上执行.我认为速度不佳是因为必须将图像数据绘制到CGContext.
如何提高绘图速度?是否可以在后台线程上绘制上下文?
我设法通过做一些事情来提高性能:
我修复了我的构建过程,以便对PNG进行iOS优化.(应用程序的内容在一个单独的项目中进行管理,该项目输出一个包.默认的包设置是针对OS X包的,它不会优化PNG).
在后台线程我:
layer.content主线程的CGImageRef上用于NSOperationQueue管理操作.
我确信有更好的方法可以做到这一点,但上述结果会带来可接受的性能.
-(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;
}
Run Code Online (Sandbox Code Playgroud)
您的动画要求是什么导致使用 UIImageView 的内置动画服务(使用 UIImages * (animationImages) 数组以及随附的animationDuration 和animationRepeatCount 不起作用?)
如果您正在快速绘制多个图像,请仔细查看 Quartz 2D。如果您正在绘制图像,然后对图像进行动画处理(移动、缩放等),那么您应该查看 Core Animation。
听起来 Quartz 2D 就是你想要的。苹果文档在这里: http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/Introduction.html
| 归档时间: |
|
| 查看次数: |
4939 次 |
| 最近记录: |