ios - 如何在 iOS 中创建带有渐变的叠加颜色

标签 ios objective-c iphone uiimageview cgcolor

背景信息:我有一个UIImageView。我通过以下方式在其图像上添加了覆盖颜色:

UIGraphicsBeginImageContext(initialImage.size);
[initialImage drawInRect:CGRectMake(0, 0, initialImage.size.width, initialImage.size.height) blendMode:kCGBlendModeNormal alpha:alphaValue];
UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, initialImage.size.width, initialImage.size.height)];
[overlayColor setFill];
[path fillWithBlendMode:kCGBlendModeMultiply alpha:1];
finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[self setImage:finalImage];

我仍然想将其添加为叠加颜色,但我希望它具有渐变。我一直在试图找出一种方法来做到这一点,但还没有真正成功。我猜,添加带有渐变的叠加颜色的方法是错误的?我不知道该怎么做。我尝试将 CGGradientLayer 作为 sublayer 添加到 UIImageView 但它不起作用。

我考虑过添加一个 UIView 并将其 backgroundColor 设置为 overlayColor,然后添加一个 CGGradientLayer 作为UIViewsublayer 作为 subview 添加到 UIImageView 中,但是,我们不应该添加 subview UIImageViews

有人可以帮我解决这个问题吗?也许我应该改变我的方法?

为我指明正确的方向也很棒!

如果这篇文章没有完全清楚,我期待您的回复并表示歉意!

预先感谢您的帮助!

编辑:CGGradientLayer的代码

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.frame;

UIColor *colorOne = [UIColor colorFromHex:self.feedColor withAlpha:(alphaValue * 0.7)];
UIColor *colorTwo = [UIColor colorFromHex:self.feedColor withAlpha:(alphaValue * 1.0)];

gradient.colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];

[self.layer insertSublayer:gradient atIndex:0];

最佳答案

我只会使用 Core Graphics 来获取输入图像,对其应用渐变叠加,然后将其传递到 UIImageView 上。像这样的事情应该达到预期的结果:

- (UIImage *)imageWithGradientOverlay:(UIImage *)sourceImage color1:(UIColor *)color1 color2:(UIColor *)color2 gradPointA:(CGPoint)pointA gradPointB:(CGPoint)pointB {

    CGSize size = sourceImage.size;

    // Start context
    UIGraphicsBeginImageContext(size);
    CGContextRef c = UIGraphicsGetCurrentContext();

    // Draw source image into context
    CGContextDrawImage(c, (CGRect){CGPointZero, size}, sourceImage.CGImage);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat gradLocs[] = {0, 1};
    NSArray *colors = @[(id)color1.CGColor, (id)color2.CGColor];

    // Create a simple linear gradient with the colors provided.
    CGGradientRef grad = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, gradLocs);
    CGColorSpaceRelease(colorSpace);

    // Draw gradient with multiply blend mode over the source image
    CGContextSetBlendMode(c, kCGBlendModeMultiply);
    CGContextDrawLinearGradient(c, grad, pointA, pointB, 0);
    CGGradientRelease(grad);

    // Grab resulting image from context
    UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return resultImg;
}

这里 sourceImage 是输入图像,color1color2 是渐变颜色,gradPointAgradPointB 是线性渐变终点(在 Core Graphics 坐标系中,左下角是 (0,0))。

这样您就不必再费力地处理图层了。如果您经常使用不同的颜色重新绘制,那么您可能需要采用使用图层的方法。

关于ios - 如何在 iOS 中创建带有渐变的叠加颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34768956/

相关文章:

iphone - 如何获取Core Data数据库的最后一条记录?

ios - SpriteKit - 生成随机节点

ios - 没有互联网的 NSNotification

iOS 6 iAd 属性和方法已弃用

ios - 导航组中的自定义后退按钮(Titanium Mobile)

iphone - 我可以将导航 Controller 添加到 ModalViewController 吗?

ios - 释放 CGMutablePathRef 对象的正确方法

ios - UIScrollView 不垂直滚动

ios - 如何在没有代码访问的情况下使用 GPX 文件在 IOS 真实设备上设置虚假 GPS 位置?

iphone - 防止 UIDocumentInteractionController PresentPreview 使我的应用程序崩溃