在 UIView 上绘制渐变在后续调用 drawRect 时不起作用:
我正在尝试调试一个真正困扰我的问题。基本上,我有一个 UIView 子类,它根据我在 init 方法中提供的颜色在 drawRect: 中向自身绘制渐变。当视图再次绘制自身时,就会出现问题。如果我在视图上调用 setNeedsDisplay,或者说视图由于内存不足警告而被删除,并且稍后会重新添加(这也会再次触发 drawRect:),那么视图的背景将完全空白。我不确定为什么重新绘制渐变会导致这种情况发生;有人知道可能出了什么问题吗?这是我在drawRect中的代码摘录:
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);
int startcolorNumComponents = CGColorGetNumberOfComponents(self.topColor.CGColor);
int endcolorNumComponents = CGColorGetNumberOfComponents(self.bottomColor.CGColor);
// get the array of floats for each of the gradient colors
const CGFloat *startComponents = CGColorGetComponents(self.topColor.CGColor);
const CGFloat *endComponents = CGColorGetComponents(self.bottomColor.CGColor);
CGFloat redStart = startComponents[0];
CGFloat greenStart = (startcolorNumComponents != 2 )? startComponents[1] : startComponents[0];
CGFloat blueStart = (startcolorNumComponents != 2 )? startComponents[2] : startComponents[0];
CGFloat startAlpha = (startcolorNumComponents != 2 )? startComponents[3] : startComponents[1];
CGFloat redEnd = endComponents[0];
CGFloat greenEnd = (endcolorNumComponents != 2) ? endComponents[1] : endComponents[0];
CGFloat blueEnd = (endcolorNumComponents != 2) ? endComponents[2] : endComponents[0];
CGFloat endAlpha = (endcolorNumComponents != 2) ? endComponents[3] : endComponents[1];
CGFloat components[8] = { redStart, greenStart, blueStart, startAlpha, redEnd, greenEnd, blueEnd, endAlpha };
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGGradientRef glossGradient = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, num_locations);
CGPoint gradientStart = CGPointMake(CGRectGetMidX(self.bounds), self.bounds.origin.x);
CGPoint gradientEnd = CGPointMake(CGRectGetMidX(self.bounds),self.bounds.size.height));
CGContextDrawLinearGradient(currentContext, glossGradient, gradientStart, gradientEnd, 0.0);
CGGradientRelease(glossGradient);
CGContextRestoreGState(currentContext);
I'm trying to debug an issue that is really stumping me. Basically, I have a UIView subclass that draws a gradient to itself in drawRect: based on colors that i provide in the init method. The problem occurs when the view draws itself again. If i call setNeedsDisplay on the view, or if say the view is removed due to an out of memory warning and it gets re-added later (which would also trigger drawRect: again) then the background of the view is completely blank. I'm not sure why redrawing a gradient would cause this to happen; does anybody know what could be going wrong? Here's an excerpt of the code I have in drawRect:
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);
int startcolorNumComponents = CGColorGetNumberOfComponents(self.topColor.CGColor);
int endcolorNumComponents = CGColorGetNumberOfComponents(self.bottomColor.CGColor);
// get the array of floats for each of the gradient colors
const CGFloat *startComponents = CGColorGetComponents(self.topColor.CGColor);
const CGFloat *endComponents = CGColorGetComponents(self.bottomColor.CGColor);
CGFloat redStart = startComponents[0];
CGFloat greenStart = (startcolorNumComponents != 2 )? startComponents[1] : startComponents[0];
CGFloat blueStart = (startcolorNumComponents != 2 )? startComponents[2] : startComponents[0];
CGFloat startAlpha = (startcolorNumComponents != 2 )? startComponents[3] : startComponents[1];
CGFloat redEnd = endComponents[0];
CGFloat greenEnd = (endcolorNumComponents != 2) ? endComponents[1] : endComponents[0];
CGFloat blueEnd = (endcolorNumComponents != 2) ? endComponents[2] : endComponents[0];
CGFloat endAlpha = (endcolorNumComponents != 2) ? endComponents[3] : endComponents[1];
CGFloat components[8] = { redStart, greenStart, blueStart, startAlpha, redEnd, greenEnd, blueEnd, endAlpha };
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGGradientRef glossGradient = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), components, locations, num_locations);
CGPoint gradientStart = CGPointMake(CGRectGetMidX(self.bounds), self.bounds.origin.x);
CGPoint gradientEnd = CGPointMake(CGRectGetMidX(self.bounds),self.bounds.size.height));
CGContextDrawLinearGradient(currentContext, glossGradient, gradientStart, gradientEnd, 0.0);
CGGradientRelease(glossGradient);
CGContextRestoreGState(currentContext);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这原来是一个内存问题。修复内存泄漏后,这种情况不再发生在我身上。
I think this turned out to be a memory problem. After fixing memory leaks this no longer happens for me.