首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ios -透明径向渐变层蒙版

ios -透明径向渐变层蒙版
EN

Stack Overflow用户
提问于 2014-05-06 19:55:40
回答 1查看 2.4K关注 0票数 0

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

代码语言:javascript
复制
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];

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

EN

回答 1

Stack Overflow用户

发布于 2014-05-06 20:46:11

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

代码语言:javascript
复制
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.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23494063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档