CGContextDrawLinearGradient导致EXC_BAD_ACCESS

Dan*_*dan 5 core-graphics objective-c linear-gradients swift bemsimplelinegraph

我正在配置我的BEMSimpleLineGraph并且除了线性渐变着色之外已经能够成功完成.在提供的示例Obj-C项目中引用此代码之后

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = {
        1.0, 1.0, 1.0, 1.0,
        1.0, 1.0, 1.0, 0.0
    };
    self.myGraph.gradientBottom = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations);
Run Code Online (Sandbox Code Playgroud)

并在Swift中将其转录为此内容:

let colorspace:CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()
        let num_locations:size_t = 2
        var locations: [CGFloat] = [0.0, 1.0]
        var components: [CGFloat] = [
            1.0, 1.0, 1.0, 1.0,
            1.0, 1.0, 1.0, 0.0
        ]

       self.myGraph.gradientBottom = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations)
Run Code Online (Sandbox Code Playgroud)

一切都正确构建但引发包含的BEMLine.m文件中的EXC_BAD_ACCESS内存错误,在此行停止

CGContextDrawLinearGradient(ctx, self.bottomGradient, CGPointZero, CGPointMake(0, CGRectGetMaxY(fillBottom.bounds)), 0);
Run Code Online (Sandbox Code Playgroud)

我已经包含了obj-c桥接头,添加了CoreGraphics框架,在Storyboard中相应ViewController的属性窗格中启用了底色,引用了Apple的开发页面以确保所有参数的正确数据类型,但我仍然会变干.在检查错误相似性时,我也意识到尝试绘制顶部线性渐变也会出现同样的错误.错误似乎在于试图绘制渐变的Obj-C代码,但我再次无法做什么.

mjr*_*der 6

我在使用Swift的BEMSimpleLineGraph时遇到了同样的问题.幸运的是,我在Github库的问题页面上找到了答案:

https://github.com/Boris-Em/BEMSimpleLineGraph/issues/105

为了解决这个问题,我刚刚在Swift类中声明了一个全局渐变,如下所示:

var gradient  : CGGradient?
Run Code Online (Sandbox Code Playgroud)

并刚刚更换了这条线

self.myGraph.gradientBottom = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations)
Run Code Online (Sandbox Code Playgroud)

有:

self.gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations)
self.myGraph.gradientBottom = self.gradient
Run Code Online (Sandbox Code Playgroud)

显然,渐变将不会在内存中保留分配,并且在库需要使用它时,它将不再可用.