26

任何人都知道如何以编程方式在具有核心图形的图像上创建透明渐变(alpha 渐变)?我需要它来显示图像并保存它。

我想获得的一个例子: 在此处输入图像描述

4

3 回答 3

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; 

要保存它,您可以在 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;
于 2012-09-21T18:55:30.630 回答
12

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

在此处输入图像描述

于 2018-03-15T14:01:52.473 回答
3

我不知道渐变的核心图形解决方案,但是如果您可以在 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;
}

这段代码改编自这个问题

或@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;
于 2012-08-04T10:59:10.217 回答