我想在 loc 550 处绘制一条带有渐变的水平线。
CGFloat colors[6] = {
138.0f / 255.0f, 1.0f,
162.0f / 255.0f, 1.0f,
206.0f / 255.0f, 1.0f};
CGFloat locations[3] = { 0.05f, 0.45f, 0.95f };
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, locations, 3);
CGColorSpaceRelease(colorSpace);
CGPoint startPoint = CGPointMake(0, 0);
CGPoint endPoint = CGPointMake(0, 550);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextAddRect(context, CGRectMake(0, 550, self.view.bounds.size.width, 1));
CGContextClip(context);
CGContextDrawLinearGradient (context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
这段代码中没有画线,Xcode 给了我这个错误:
CGContextAddRect: invalid context 0x0