【问题标题】:ios - Transparent radial Gradient Layer Maskios - 透明径向渐变图层蒙版
【发布时间】:2014-06-23 00:42:09
【问题描述】:

我使用CAGradientLayer 作为图像的图层。我正在使用此代码:

UIImage *image = [UIImage imageNamed:@"perfect.jpg"];

[testImage setImage:image];

CAGradientLayer *myLayer = [CAGradientLayer layer];

myLayer.anchorPoint = CGPointZero;

//starts in bottom left
myLayer.startPoint = CGPointMake(0.0f,0.5f);

//ends in top right
myLayer.endPoint = CGPointMake(0.5f, 0.0f);

UIColor *outerColor = [UIColor colorWithWhite:1.0 alpha:0.0];
UIColor *innerColor = [UIColor colorWithWhite:1.0 alpha:1.0];

//an array of colors that dictatates the gradient(s)
myLayer.colors = @[(id)outerColor.CGColor, (id)outerColor.CGColor, (id)innerColor.CGColor, (id)innerColor.CGColor];

//these are percentage points along the line defined by our startPoint and endPoint and correspond to our colors array. The gradient will shift between the colors between these percentage points.
myLayer.locations = @[@0.0, @0.15, @0.5, @1.0f];

myLayer.bounds = CGRectMake(0, 0, CGRectGetWidth(testImage.bounds), CGRectGetHeight(testImage.bounds));

testImage.layer.mask = myLayer;

[self.view addSubview:testImage];

但渐变仅应用于我的图像的侧面。我想要的是将渐变应用为圆形或径向渐变。我该怎么做?

【问题讨论】:

标签: ios cagradientlayer


【解决方案1】:

您可以使用名称为 CIVignette 的 CIFilter 获得径向渐变。尝试使用下面的代码。

UIImage *image = [UIImage imageNamed:@"perfect.jpg"];
CIFilter *vignette = [CIFilter filterWithName:@"CIVignette"];
[vignette setValue:image forKey:kCIInputImageKey];
[vignette setValue:@(1) forKey:@"inputIntensity"];
[vignette setValue:@(30) forKey:@"inputRadius"];

CIImage *outputImage = [vignette outputImage];
CGImageRef cgimg = [ [CIContext contextWithOptions:nil] createCGImage:outputImage
                                 fromRect:[outputImage extent]];
UIImage *newImage = [UIImage imageWithCGImage:cgimg];
testImage.image = newImage;
[self.view addSubview:testImage];

更多信息可以参考this tutorial.

【讨论】:

    猜你喜欢
    • 2012-01-12
    • 2011-03-20
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2013-12-05
    • 2020-12-09
    相关资源
    最近更新 更多