3

I am creating a SVG coloring application an i have a custom CAShapeLayer for drawing radial gradients. In my application i usually have around 300 of these layers for a SVG. My code works fine. But the problem is I run out of memory really quickly when drawing gradients to the CAShapeLayer using draw(in ctx: CGContext) method. I want to clear my memory so that application will not crash.

Here is a simpler version of my custom CAShapeLayer code:

import Foundation
import QuartzCore
class MyCAShapeLayer: CAShapeLayer {
    public func updateLayer() {
        self.fillColor = nil
        self.setNeedsDisplay()
    }
    override func draw(in ctx: CGContext) {
        ctx.saveGState()
        if (self.path != nil) {
            ctx.addPath(self.path!)
            ctx.clip()
        }
        let numberOfLocations: Int = 2;
        let gradientLocations: [CGFloat] = [0.0, 1.0]
        let gradientComponents: [CGFloat] = [1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0];
        let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
        let gradient: CGGradient = CGGradient(colorSpace: colorSpace, colorComponents: gradientComponents, locations: gradientLocations, count: numberOfLocations)!
        let gradientCenter: CGPoint = CGPoint(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)
        let gradientRadius: CGFloat = min(self.bounds.size.width, self.bounds.size.height)
        ctx.drawRadialGradient(gradient, startCenter: gradientCenter, startRadius: 0, endCenter: gradientCenter, endRadius: gradientRadius / 2, options: .drawsAfterEndLocation)
        ctx.restoreGState()
    }
}

Here is my ViewController.swift: This is not the actual code. But this is what simply happens under the hood when rendering SVG. I create a CAShapeLayer for each SVG element and add it to view's sub layer. Meanwhile I store it in an array for future operations.

for i in 0...35 {
    let layer: MyCAShapeLayer = MyCAShapeLayer()
    let rect: CGRect = CGRect(x: i * 10, y: i * 10, width: 700 - (i * 20), height: 700 - (i * 20))
    let path: CGPath = CGPath(rect: rect, transform: nil)
    layer.path = path
    layer.fillColor = UIColor.white.cgColor
    layer.strokeColor = UIColor.black.cgColor
    layer.bounds = path.boundingBoxOfPath
    layer.frame = layer.bounds
    self.view.layer.addSublayer(layer)
    self.shapeLayerList.append(layer)
}

And OnTap I draw gradient on each layer.

self.shapeLayerList.forEach {
    shapeLayer in
    shapeLayer.updateLayer()
}

Here memory will increase rapidly and when having around 300 layers application will run out of memory.

Is there a way to manage my memory when drawing? Any suggestion will by appreciated.

Nilupul Sandeepa
  • 748
  • 6
  • 20

0 Answers0