gpt4 book ai didi

ios - 蒙版图像并填充颜色

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

我想用颜色遮盖 image 并填充选区和其余部分。我正在使用 CGImageCreateWithMaskingColors,但我不知道如何用另一种颜色填充 image

这是我的代码的开始

UIImage *source = [UIImage imageNamed:@"nb.png"];

const CGFloat myMaskingColors[6] = {0,110,0,110,0,110};
CGImageRef imageRef = CGImageCreateWithMaskingColors(source.CGImage, myMaskingColors);
UIImage* imageB = [UIImage imageWithCGImage:imageRef];
UIImageView *imageView = [[UIImageView alloc]initWithImage:imageB];

感谢您的帮助编辑:我想我不清楚,我想选择一种颜色,其余选择另一种颜色

最佳答案

如果你想在图像上应用 2 种颜色,那么你可以像这样在图像上应用渐变

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

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

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

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

CGGradientRelease(gradient);
CGColorSpaceRelease(space);

return gradientImage;
}

关于ios - 蒙版图像并填充颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16646884/

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