【问题标题】:Transparent (alpha) gradient with Core Graphic iPhone使用 Core Graphic iPhone 的透明 (alpha) 渐变
【发布时间】:2012-08-02 05:16:15
【问题描述】:

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

我想获得的一个例子:

【问题讨论】:

    标签: ios core-graphics gradient alpha


    【解决方案1】:

    您可以使用 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;
    

    【讨论】:

    • 什么是_fileTypeScrollView?
    • @DanielH。糟糕,错字。它应该是 myImageView。固定。
    • 请问有 swift 版本吗?
    • 我在处理常规 UIView 时使用了所有相同的代码,除了最后一行。而不是view.layer.mask = gradientLayer,我不得不使用view.layer.insertSublayer(topGradient, atIndex: 0)。在我使用这种方法之前,它无法正常工作。
    【解决方案2】:

    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
    

    【讨论】:

    • 很好的答案,在 Swift 5 中仍然适用于我
    【解决方案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;
    }
    

    这段代码改编自this question

    或@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;
    

    【讨论】:

    • 是的,有可能,但我必须保存结果图像,而且我没有固定的渐变起点和终点,我希望它是参数化的。
    • 请问有 swift 版本吗?
    猜你喜欢
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    相关资源
    最近更新 更多