Created
November 16, 2011 17:15
-
-
Save ksol/1370705 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Caller, a subclass of UITableViewCell. Only have a method | |
| - (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); | |
| } | |
| // For Peter Hosey : | |
| (gdb) po startColor | |
| <CGColor 0x1deca0> [<CGColorSpace 0x1d3280> (kCGColorSpaceDeviceGray)] ( 1 1 ) | |
| Current language: auto; currently objective-c | |
| (gdb) po endColor | |
| <CGColorSpace 0x1bf120> (kCGColorSpaceDeviceRGB) | |
| (gbd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment