我有一個編譯時沒有問題的代碼。它在iPhone模擬器上運行良好,但在我的設備上,我得到了一個EXC_BAD_ACCESS。CG Gradient在模擬器上運行,但不在iPhone上
這發生在繪製漸變的輔助函數中。我跟着this tutorial來做到這一點。我的代碼如下:。
- (void) drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef whiteColor = [UIColor whiteColor].CGColor;
CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0
green:230.0/255.0
blue:230.0/255.0
alpha:1.0].CGColor;
CGColorRef separatorColor = [UIColor colorWithRed:208.0/255.0
green:208.0/255.0
blue:208.0/255.0
alpha:1.0].CGColor;
CGRect paperRect = self.bounds;
CGRect nameRect = self.nameLabel.frame;
CGPoint sepStartPoint = CGPointMake(nameRect.origin.x,
nameRect.origin.x + nameRect.size.height + 2);
CGPoint sepEndPoint = CGPointMake(nameRect.origin.x + nameRect.size.width,
nameRect.origin.x + nameRect.size.height + 2);
drawLinearGradient(context, paperRect, lightGrayColor, whiteColor);
draw1PxStroke(context, sepStartPoint, sepEndPoint, separatorColor);
}
// Callee, where the problem is
void drawLinearGradient(CGContextRef context,
CGRect rect,
CGColorRef startColor,
CGColorRef endColor)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = [NSArray arrayWithObjects:
(__bridge id)startColor,
(__bridge id)endColor,
nil]; // Here is the line
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
(__bridge CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
Xcode中的亮點第12行(具有nil];
爲錯誤行
彼得Hosey,這裏的調試器輸出:
(gdb) po startColor
<CGColor 0x1deca0> [<CGColorSpace 0x1d3280> (kCGColorSpaceDeviceGray)] (1 1)
Current language: auto; currently objective-c
(gdb) po endColor
<CGColorSpace 0x1bf120> (kCGColorSpaceDeviceRGB)
(gbd)
我模擬器(和iPhone)在iOS 5上運行。
什麼可能導致此崩潰?
這可能意味着startColor或endColor中的一個是懸掛指針;你可以顯示調用drawLinearGradient的代碼嗎? – Tommy
我更新了調用代碼的要點 – ksol
如果您在調試器控制檯中鍵入'po startColor'和'po endColor',您會得到什麼? –