iOS更改图片一半的颜色

3
我有一张这样的图片... enter image description here 我需要实现的是更改这个图像下半部分的颜色,就像这个图像一样... enter image description here 我尝试过使用CAGradientLayer、Masking等方法,但没有成功得到结果。
任何帮助都将不胜感激。谢谢。
更新1:
在使用以下代码后,我得到了这个结果。还有一件事要补充,那就是在使用前调整了图像大小。 enter image description here 更新2: enter image description here

为什么不在照片编辑器中编辑图像,然后将其添加回项目中呢?如果您想在两个图像之间切换...只需使用两个图像,这样就可以产生您想要的效果了。 - Robert J. Clegg
实际上,图像的大小经常会发生变化。 - Ahmed Z.
你是使用一个标签来展示文本"There",还是在图像本身中呢?如果你是使用标签来展示文本,那么为UILabel提供必要的背景颜色如何? - borncrazy
是的,它是一个标签...但周围有很多填充吗? - Ahmed Z.
https://dev59.com/5Wct5IYBdhLWcg3wCZIT#12399760 - Arun
4个回答

9

这让我面临一些挑战,但是我来试着翻译一下:

返回底部涂有某种颜色的图像的方法

- (UIImage *)image:(UIImage *)image withBottomHalfOverlayColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.f, 0.f, image.size.width, image.size.height);

    if (UIGraphicsBeginImageContextWithOptions) {
        CGFloat imageScale = 1.f;
        if ([self respondsToSelector:@selector(scale)])
            imageScale = image.scale;
        UIGraphicsBeginImageContextWithOptions(image.size, NO, imageScale);
    }
    else {
        UIGraphicsBeginImageContext(image.size);
    }

    [image drawInRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(context, kCGBlendModeSourceIn);

    CGContextSetFillColorWithColor(context, color.CGColor);

    CGRect rectToFill = CGRectMake(0.f, image.size.height*0.5f, image.size.width, image.size.height*0.5f);
    CGContextFillRect(context, rectToFill);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

使用示例:

    UIImage *image = [UIImage imageNamed:@"q.png"];
    image = [self image:image withBottomHalfOverlayColor:[UIColor cyanColor]];
    self.imageView.image = image;

我的结果:在此输入图片描述在此输入图片描述


刚刚添加了问题,请从这里获取翻译后的文本。 - Ahmed Z.
这是我用于调整大小的代码:UIImage *bgImage = [[UIImage imageNamed:@"TM-Bubbles-Blue"] resizableImageWithCapInsets: UIEdgeInsetsMake(30, 32, 30, 23) resizingMode: UIImageResizingModeStretch]; - Ahmed Z.
尝试交换顺序,先改变颜色再调整大小。 - haawa
这对我非常有用,非常感谢!我已将其翻译为Swift,将在下面发布。 - Coder1224

6

在 Swift 4 中,有一些变化:

func withBottomHalfOverlayColor(myImage: UIImage, color: UIColor) -> UIImage
  {
    let rect = CGRect(x: 0, y: 0, width: myImage.size.width, height: myImage.size.height)


    UIGraphicsBeginImageContextWithOptions(myImage.size, false, myImage.scale)
    myImage.draw(in: rect)

    let context = UIGraphicsGetCurrentContext()!
    context.setBlendMode(CGBlendMode.sourceIn)

    context.setFillColor(color.cgColor)

    let rectToFill = CGRect(x: 0, y: myImage.size.height*0.5, width: myImage.size.width, height: myImage.size.height*0.5)
    context.fill(rectToFill)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage!
  }

0

Swift 5 扩展

extension UIImage {
    func withBottomHalfOverlayColor(color: UIColor) -> UIImage
      {
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)


        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        draw(in: rect)

        let context = UIGraphicsGetCurrentContext()!
        context.setBlendMode(CGBlendMode.sourceIn)

        context.setFillColor(color.cgColor)

        let rectToFill = CGRect(x: 0, y: size.height*0.5, width: size.width, height: size.height*0.5)
        context.fill(rectToFill)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage!
      }
}

0

你可以使用一些技巧:

  • 使用2个不同的图像并更改整个背景。
  • 使用一个背景颜色(浅蓝色)和2个图像,其中一个图像底部半透明。

我需要计算背景图像的大小,然后将其放置在视图上。 - Ahmed Z.
根据我的经验,绘制如此精确和复杂的组合视图比编写非常复杂的绘图代码更容易通过使用图像和类似这样的技巧来实现。此外,您应该考虑将该小部件制作为两个单独的视图或类似的东西。 - txulu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接