I am using method described here to set a gradient background for vanilla UIView
instance without creating a subclass. However the view comes out completely black. Here's my code:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 315.0f, 44.0f)];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, 315.0, 44.0f, 8, 4 * 315, colorSpace, kCGImageAlphaNoneSkipFirst);
// Colors only for debugging purposes
CGColorRef startColor = (__bridge CGColorRef)[UIColor greenColor];
CGColorRef endColor = (__bridge CGColorRef)[UIColor redColor];
CFMutableArrayRef colors = CFArrayCreateMutable(kCFAllocatorDefault, 2, &kCFTypeArrayCallBacks);
CFArrayAppendValue(colors, startColor);
CFArrayAppendValue(colors, endColor);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, colors, (const CGFloat []){0.25f, 0.75f});
CGContextDrawLinearGradient(context, gradient, CGPointZero, CGPointMake(0.0f, 44.0f), 0);
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage *backgroundGradient = [UIImage imageWithCGImage:cgImage];
[view setBackgroundColor:[UIColor colorWithPatternImage:backgroundGradient]];
CFRelease(colors);
CGGradientRelease(gradient);
CGImageRelease(cgImage);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
return view;
I believe something might be wrong with the allocation of context since the created images are returned of desired size (315x44). What did I miss? What else can I check that would help me debug this problem?