我知道如何创建渐变UIImage,颜色从上到下变化。如下图所示:
[self linearGradientImageFromColors:@[(__bridge id)[UIColor blueColor].CGColor, (__bridge id)[UIColor purpleColor].CGColor, (__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor]]
- (UIImage *)linearGradientImageFromColors:(NSArray*)colorArray
{
CGSize textSize = self.bounds.size;
CGFloat width = textSize.width;
CGFloat height = textSize.height;
CGSize size = CGSizeMake(width, height);
CAGradientLayer *layer = [CAGradientLayer layer];
layer.frame = CGRectMake(0, 0, size.width, size.height);
layer.colors = colorArray;
UIGraphicsBeginImageContextWithOptions(size,NO,0);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
但是我现在正试图找出如何实现以下类型:每个角落的颜色不同:
我有点能够通过绘制4个单独的图像来实现更接近我想要的东西,其中每个图像是一个角落中的渐变颜色,而另一个角落是clearColor,然后组合所有4个图像以获得结果。然而,这给了我一个与我想要的不同(更暗)的结果。我哪里错了?
4角渐变尝试代码:
[self corneredGradientImageFromColors:@[[UIColor blueColor], [UIColor purpleColor], [UIColor yellowColor], [UIColor redColor]]]
- (UIImage *)corneredGradientImageFromColors:(NSArray*)colorArray
{
CGSize textSize = self.bounds.size;
CGFloat width = textSize.width;
CGFloat height = textSize.height;
CGSize size = CGSizeMake(width, height);
//CGContextRef context = UIGraphicsGetCurrentContext();
CAGradientLayer *layer1 = [CAGradientLayer layer];
layer1.frame = CGRectMake(0, 0, size.width, size.height);
layer1.colors = @[(__bridge id)((UIColor*)[colorArray objectAtIndex:0]).CGColor, (__bridge id)[UIColor clearColor].CGColor];
layer1.startPoint = CGPointMake(0.0, 0.0);
layer1.endPoint = CGPointMake(1.0, 1.0);
UIGraphicsBeginImageContextWithOptions(size,NO,0);
[layer1 renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imagelayer1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CAGradientLayer *layer2 = [CAGradientLayer layer];
layer2.frame = CGRectMake(0, 0, size.width, size.height);
layer2.colors = @[(__bridge id)[UIColor clearColor].CGColor, (__bridge id)((UIColor*)[colorArray objectAtIndex:1]).CGColor];
layer2.startPoint = CGPointMake(0.0, 1.0);
layer2.endPoint = CGPointMake(1.0, 0.0);
UIGraphicsBeginImageContextWithOptions(size,NO,0);
[layer2 renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imagelayer2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CAGradientLayer *layer3 = [CAGradientLayer layer];
layer3.frame = CGRectMake(0, 0, size.width, size.height);
layer3.colors = @[(__bridge id)((UIColor*)[colorArray objectAtIndex:2]).CGColor, (__bridge id)[UIColor clearColor].CGColor];
layer3.startPoint = CGPointMake(0.0, 1.0);
layer3.endPoint = CGPointMake(1.0, 0.0);
UIGraphicsBeginImageContextWithOptions(size,NO,0);
[layer3 renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imagelayer3 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CAGradientLayer *layer4 = [CAGradientLayer layer];
layer4.frame = CGRectMake(0, 0, size.width, size.height);
layer4.colors = @[(__bridge id)[UIColor clearColor].CGColor, (__bridge id)((UIColor*)[colorArray objectAtIndex:3]).CGColor];
layer4.startPoint = CGPointMake(0.0, 0.0);
layer4.endPoint = CGPointMake(1.0, 1.0);
UIGraphicsBeginImageContextWithOptions(size,NO,0);
[layer4 renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imagelayer4 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(size);
[imagelayer1 drawAtPoint:CGPointZero];
[imagelayer2 drawAtPoint:CGPointZero];
[imagelayer3 drawAtPoint:CGPointZero];
[imagelayer4 drawAtPoint:CGPointZero];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
这实现了这个目标:
这不是所提及的副本。我正在尝试创建一个UIImage,并且提到的副本既不会创建UIImage,也不会解决该问题。