Understanding iOS view Compositing小结

本文分享了几个关键技巧来优化iOS应用中的视图性能,包括合理使用视图的opaque.mask和shadow属性、注意图像的alpha通道及尺寸、利用Instruments进行性能分析以及对比Quartz与CoreAnimation的性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

每一帧的数据准备尽量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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值