iOS开发之drawRect内存优化

文章预读

内存恶鬼drawRect - 谈画图功能的内存优化

根据文中提到的解决内存问题有这重要的两点:
①尽量不用drawRect进行绘图
②如果要用,也尽量减小画布

第一点中我们采用何种方式来代替呢,最好是用CAShapLayer,然后配合UIBezierPath来做。

贴一段代码看下:

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(point1X, point1Y)];
    [path addLineToPoint:CGPointMake(point2X, point2Y)];
    [path addLineToPoint:CGPointMake(point3X, point3Y)];
    [path addLineToPoint:CGPointMake(point4X, point4Y)];
    [path closePath];
    //[path fill];
    
    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor = randomColor.CGColor;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.strokeColor = [UIColor clearColor].CGColor;
    shapeLayer.lineWidth = path.lineWidth;
    [self.layer addSublayer:shapeLayer];

附录

UIBezierPath

你可能感兴趣的:(iOS开发之drawRect内存优化)