gpt4 book ai didi

ios - 带有 Core Graphic iPhone 的透明(alpha)渐变

转载 作者:IT王子 更新时间:2023-10-29 08:00:44 26 4
gpt4 key购买 nike

有人知道如何使用 Core Graphic 在图像上以编程方式创建透明渐变(alpha 渐变)吗?我需要它来显示图像并保存它。

我想获得的一个例子: enter image description here

最佳答案

您可以使用 QuartzCore 将 mask (具有渐变透明度)应用到 UIImageView:

UIImageView *myImageView = ["the image view"];
CAGradientLayer *l = [CAGradientLayer layer];
l.frame = myImageView.bounds;
l.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:0] CGColor], (id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor], nil];

l.startPoint = CGPointMake(0.0, 0.0f);
l.endPoint = CGPointMake(1.0f, 1.0f);

//you can change the direction, obviously, this would be top to bottom fade
myImageView.layer.mask = l;

要保存它,您可以在 Stack Overflow 上轻松找到答案,只需搜索如何将 Canvas 的内容保存到 UIImage。

或@Zazu 要求的 Swift 版本:

// Whatever you image view is, obviously not hardcoded like this
let imageView = UIImageView.init(image: UIImage(named: "Image"))
imageView.frame = CGRectMake(0, 0, 320, 480)
self.view.addSubview(imageView)
// Create the gradient layer
let gradientLayer = CAGradientLayer.init()
gradientLayer.frame = imageView.bounds
gradientLayer.colors = [
UIColor.init(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0).CGColor,
UIColor.init(colorLiteralRed: 0, green: 0, blue: 0, alpha: 1).CGColor]
// Whatever direction you want the fade. You can use gradientLayer.locations
// to provide an array of points, with matching colors for each point,
// which lets you do other than just a uniform gradient.
gradientLayer.startPoint = CGPointMake(1.0, 0.0);
gradientLayer.endPoint = CGPointMake(0.0, 0.0);
// Use the gradient layer as the mask
imageView.layer.mask = gradientLayer;

关于ios - 带有 Core Graphic iPhone 的透明(alpha)渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11807876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com