每一帧的数据准备尽量16ms内完成操作!
1. 视图的opaque.mask,shadow都会增加额外开销,因此如果不需要这些效果时就禁用,提高效率同时省电
2. 绘制opaque = YES的图像时注意图像是否带alpha通道,图像素材尽量选择合适的大小。
3. 使用Instruments中的Core Animation来调试看是否有地方带来了额外的绘制开销
4. Quartz的效率比Core Animation更高效并节省内存
几段示例代码
Resize
UIImage *image = [self loadImage];
CGSize s = image.size;
CGRect r = layer.bounds;
CGFloat scale = MIN(r.szie.width / s.width, r.size.height / s.height);
s.width *= scale; s.height *= scale;
r.origin.x += (r.size.width - s.width) *.5;
r.size.width =s.width;
r.origin.y += (r.size.height - s.height) * .5;
r.size.height = s.height;
CGContextScaveGState(ctx);
CGContextDrawImage(ctx, r, image.CGImage);
CGContextRestroGState(ctx);
Mask
CGRect rect = layer.bounds;
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:10.0] addClip];
[image drawInRect:rect];
Shadow
CALayer *layer = view.layer;
layer.bounds = sublayer_bounds;
layer.backgroundColor = random_color();
layer.shadowOpacity = 0.5
layer.shdowRadius = 10;
layer.shdowOffset = CGSizeMake(0,10);
CGPathRef shdowPath =
[UIBezierPath bezierPathWithRect:sublayer_bounds].CGPath;
sublayer.shadowPath = shadowPath;
UIImage *image = [self loadImage];
UIColor *color = [UIColor colorWithWhite:0 alpha:0.5];
CGContextSetShadowWithColor(ctx,
CGSizeMake(0, 10), /* offset */
10, /*blur radius*/
color.CGColor);
CGContextDrawImage(ctx, r, image.CGImage);