【问题标题】:Applying gradient to UIImage - how to remove color invertion?将渐变应用于 UIImage - 如何消除颜色反转?
【发布时间】:2013-04-23 12:34:39
【问题描述】:

我正在对UIImage 应用渐变,我希望它在底部变暗,然后在中间慢慢变成透明或浅灰色。 大部分都可以,但我有一个问题,我的图像颜色在这个渐变下的某些地方会反转。这看起来很烦人。我该如何解决?

为了您的方便,这是我的方法。

我尝试选择不同的混合模式,但没有帮助。另一个问题,如何让底部的黑色更加密集?

此方法无需任何额外代码即可工作。 开始颜色是[UIColor blackColor],结束颜色是[UIColor clearColor]

- (UIImage *)imageWithGradient:(UIImage *)img startColor:(UIColor *)color1 endColor:(UIColor *)color2 {
    UIGraphicsBeginImageContextWithOptions(img.size, NO, img.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, img.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetBlendMode(context, kCGBlendModeDarken); // tried normal here, same results


    CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
    CGContextDrawImage(context, rect, img.CGImage);

    // Create gradient
    NSArray *colors = [NSArray arrayWithObjects:(id)color1.CGColor, (id)color2.CGColor, nil];
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);

    // Apply gradient
    CGContextClipToMask(context, rect, img.CGImage);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0, img.size.height / 2.0), 0);
    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGGradientRelease(gradient);
    CGColorSpaceRelease(space);

    return gradientImage;
}

【问题讨论】:

    标签: ios objective-c ios5 ios6


    【解决方案1】:

    试试这个

    // Set image over layer
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = imageView.frame;
    
    // Add colors to layer
    UIColor *centerColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.25];
    UIColor *endColor = [UIColor grayColor];
    gradient.colors = [NSArray arrayWithObjects:
                                   (id)[endColor CGColor],
                                   (id)[centerColor CGColor],
                                   (id)[endColor CGColor],
                                   nil];
    
    [imageView.layer insertSublayer:gradient atIndex:0];
    

    只需根据您的要求更改 gradient.colors 数组即可。

    【讨论】:

    • 对于那些试图在 Swift 中实现相同效果的人(带有渐变的 imageView),请确保您的颜色数组的类型是 [CGColor] 而不是 [UIColor]。我没有收到任何错误,而是忽略了此代码(视图不受影响)。找到它in this answer
    【解决方案2】:

    Swift 中的 Navnath 答案

    var gradient = CAGradientLayer()
    gradient.frame = imageView.frame
    gradient.colors = [
        UIColor(white: 0, alpha: 0.25).CGColor,
        UIColor.grayColor().CGColor, 
        UIColor(white: 0, alpha: 0.25).CGColor
    ]
    imageView.layer.insertSublayer(gradient, atIndex: 0)
    

    希望对你有用

    【讨论】:

    • 谢谢 ;) 但它是 grayColor 而不是 grayColor
    【解决方案3】:

    对 Navnath 和 Francis 的回答稍作修正... gradient.frame 需要设置为 imageView.bounds:

    gradient.frame = imageView.bounds
    

    【讨论】:

      【解决方案4】:

      尝试仅更改 alpha 以获得所需的结果。将图层放在现有图像的顶部。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-14
        • 1970-01-01
        • 1970-01-01
        • 2022-12-30
        相关资源
        最近更新 更多