gpt4 book ai didi

iOS 改变一半图像的颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:17 24 4
gpt4 key购买 nike

我有一张看起来像这样的图片......

enter image description here

我要实现的是我必须改变这张图片下半部分的颜色。像这个.. enter image description here

我试过使用 CAGradientLayerMasking 但没有成功..

任何帮助都会很棒..提前致谢..

更新 1

这就是 em 在这段代码之后得到的结果。我需要添加的一件事是在使用之前调整了图像的大小。

enter image description here

更新 2

enter image description here

最佳答案

你给了我一些很好的挑战,但是你开始吧:

返回下半部分用某种颜色着色的图像的方法

- (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;

我的结果:enter image description here enter image description here

关于iOS 改变一半图像的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22960945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com