Pab*_*ect 24 gradient alpha core-graphics ios
任何人都知道如何以核心图形在图像上以编程方式创建透明渐变(alpha渐变)?我需要它来显示图像并保存它.
我想要获得的一个例子:

Mat*_*rst 45
您可以使用QuartzCore将蒙版(具有渐变透明度)应用于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;
Run Code Online (Sandbox Code Playgroud)
要保存它,您可以在Stack Overflow上轻松找到答案,只需搜索如何将画布内容保存到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;
Run Code Online (Sandbox Code Playgroud)
小智 5
Swift 3-这对我有用。我只需要底部图像的20%即可实现渐变透明。
// Create the gradient layer
let gradientLayer = CAGradientLayer()
gradientLayer.frame = imageView.bounds
gradientLayer.colors = [
UIColor.white.withAlphaComponent(1).cgColor,
UIColor.white.withAlphaComponent(0).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 = CGPoint(x: 0.0, y: 0.80)
gradientLayer.endPoint = CGPoint(x: 0.0, y: 1.0)
// Use the gradient layer as the mask
imageView.layer.mask = gradientLayer
Run Code Online (Sandbox Code Playgroud)

我不知道渐变的核心图形解决方案,但如果你可以在 Photoshop 中制作一个图像,该图像从右侧开始为白色,并在左侧渐变为 Alpha,你可以将其覆盖在内容图像的顶部
要覆盖图像,你会做类似的事情
+ (UIImage*) addFadeOutToImage:(UIImage*)sourceImage
{
UIGraphicsBeginImageContext(sourceImage.size);
[sourceImage drawAtPoint:CGPointZero];
UIImage* fadeAlphaImage = [UIImage imageNamed:@"fadedAlphaImage.png"];
CGPoint fadeOutStartPoint = CGPointMake(sourceImage.size.width - fadeAlphaImage.size.width, 0);
//assumes the fade image is the same height as the source
[fadeAlphaImage drawAtPoint:fadeOutStartPoint];
UIImage* fadeOutImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return fadeOutImage;
}
Run Code Online (Sandbox Code Playgroud)
这段代码改编自这个问题
或者按照@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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12285 次 |
| 最近记录: |