我从这里开始:http ://chinkisingh.com/2013/03/03/draw-on-iphoneipad-screen-using-bezier-paths-core-graphics-ios-app-development/
我有一个 UIBezierPath,我想对其应用渐变并应用到现有图像,看看下面的代码是否有帮助
CGRect r = CGRectMake(0, 0, HEART_SIZE, HEART_SIZE);
UIBezierPath *heart = [Bezier heartShape:r]; //this is only in my case
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UIColor *darkPink = [UIColor colorWithHue:0.915 saturation:1.000 brightness:0.941 alpha:1.000];
UIColor *lightPink = [UIColor colorWithHue:0.917 saturation:0.647 brightness:1.000 alpha:1.000];
NSArray *gradientColors = [NSArray arrayWithObjects:
(id)darkPink.CGColor,
(id)lightPink.CGColor,
(id)darkPink.CGColor, nil];
CGFloat gradientLocations[] = {0, 0.5, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
CGColorSpaceRelease(colorSpace);
UIGraphicsBeginImageContext(r.size);
heart.lineCapStyle = kCGLineCapRound;
heart.lineWidth = 10.0f;
[[UIColor blackColor] setStroke];
[[UIColor redColor] setFill];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
[heart stroke]; //removed the black stroke around
[heart fill];
CGContextAddPath(context, heart.CGPath);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, CGPointMake(10, 10), CGPointMake(210, 210), kCGGradientDrawsAfterEndLocation); //check that gradient is drawn in the right place
CGGradientRelease(gradient);
UIImage *theRightImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();