我正在配置我的 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);
并在 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)
一切都正确构建,但在包含的 BEMLine.m 文件中抛出 EXC_BAD_ACCESS 内存错误,并在此行停止
CGContextDrawLinearGradient(ctx, self.bottomGradient, CGPointZero, CGPointMake(0, CGRectGetMaxY(fillBottom.bounds)), 0);
我已经包含了 obj-c 桥接 header ,添加了 CoreGraphics 框架,在 Storyboard 中相应 ViewController 的属性 Pane 中启用了底部颜色,引用了 Apple 的开发页面以确保所有参数的数据类型正确,但我仍在继续干起来。在检查错误相似性时,我还意识到在尝试绘制顶部线性渐变时也会出现同样的错误。错误似乎出在尝试绘制渐变的 Obj-C 代码中,但我又一次不知所措。
最佳答案
我在使用 Swift 的 BEMSimpleLineGraph 时遇到了同样的问题。幸运的是,我在 Github 库的问题页面上找到了答案:
https://github.com/Boris-Em/BEMSimpleLineGraph/issues/105
为了解决这个问题,我只是在 Swift 类中声明了一个全局渐变,如下所示:
var gradient : CGGradient?
只是替换了行
self.myGraph.gradientBottom = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations)
与:
self.gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations)
self.myGraph.gradientBottom = self.gradient
显然,否则梯度将不会保留在内存中,并且在库需要使用它时,它不再可用。
关于objective-c - CGContextDrawLinearGradient 导致 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29332855/