                 CGContextSetBlendMode(context, CGBlendMode.DestinationOut)
                 layers[i].layer.renderInContext(context)
             }
+            CGContextRestoreGState(context)
         }
         imageView.image = UIGraphicsGetImageFromCurrentImageContext()
         UIGraphicsEndImageContext()

        imageView.image?.drawInRect(CGRect(origin: .zero, size: image.size))
        for i in range {
            updateMaskColor(layers[i].maskColor, fPlus: layers[i].fPlus)
            if let maskImage = maskImage {
                let rc = CGRect(origin: .zero, size: image.size)
                UIGraphicsBeginImageContext(image.size)

            shapeLayer.path = nil
            let layer = createShapeLayer()
            layer.path = builder.end()
            print("Ended")
            layers.removeRange(index..<layers.count)
            layers.append(Layer(layer: layer, maskColor: maskColor, fPlus: segment.selectedSegmentIndex==2))
            redo()
