ios – 如何在不透明的UIImage上用另一种替换给定的颜色[复制]

ios – 如何在不透明的UIImage上用另一种替换给定的颜色[复制],第1张

概述参见英文答案 > How can I change image tintColor in iOS and WatchKit                                    18个 >             ios: change the colors of a UIImage                                    7个 我想改变不透明UIIm 参见英文答案 > How can I change image tintColor in iOS and WatchKit                                    18个
>             ios: change the colors of a UIImage                                    7个
我想改变不透明UIImage的颜色.我的原始图片如下:

我想将图像转换为以下格式

所以,基本上我想将图像中的红色转换为黑色(任何其他颜色)颜色.添加以上两张图片是为了更好地理解.

解决方法 我看不到’重复’上的任何答案(这个问题不应该被标记为重复),它会让你用另一种颜色替换给定的颜色并处理不透明的图像,所以我决定添加一个将.

我创建了一个UIImage类别,它基本上是通过循环每个像素并检测它与给定颜色的接近程度,并将其与替换颜色混合(如果是).

这适用于具有透明度和不透明背景的图像.

@implementation UIImage (UIImagecolorReplacement)-(UIImage*) imageByReplacingcolor:(UIcolor*)sourcecolor withMinTolerance:(CGfloat)minTolerance withMaxTolerance:(CGfloat)maxTolerance withcolor:(UIcolor*)destinationcolor {    // components of the source color    const CGfloat* sourceComponents = CGcolorGetComponents(sourcecolor.CGcolor);    UInt8* source255Components = malloc(sizeof(UInt8)*4);    for (int i = 0; i < 4; i++) source255Components[i] = (UInt8)round(sourceComponents[i]*255.0);    // components of the destination color    const CGfloat* destinationComponents = CGcolorGetComponents(destinationcolor.CGcolor);    UInt8* destination255Components = malloc(sizeof(UInt8)*4);    for (int i = 0; i < 4; i++) destination255Components[i] = (UInt8)round(destinationComponents[i]*255.0);    // raw image reference    CGImageRef rawImage = self.CGImage;    // image attributes    size_t wIDth = CGImageGetWIDth(rawImage);    size_t height = CGImageGetHeight(rawImage);    CGRect rect = {CGPointZero,{wIDth,height}};    // bitmap format    size_t bitsPerComponent = 8;    size_t bytesPerRow = wIDth*4;    CGBitmAPInfo bitmAPInfo = kCGImageAlphaPremultiplIEdLast | kCGBitmapByteOrder32Big;    // data pointer    UInt8* data = calloc(bytesPerRow,height);    CGcolorSpaceRef colorSpace = CGcolorSpaceCreateDeviceRGB();    // create bitmap context    CGContextRef ctx = CGBitmapContextCreate(data,wIDth,height,bitsPerComponent,bytesPerRow,colorSpace,bitmAPInfo);    CGContextDrawImage(ctx,rect,rawImage);    // loop through each pixel's components    for (int byte = 0; byte < bytesPerRow*height; byte += 4) {        UInt8 r = data[byte];        UInt8 g = data[byte+1];        UInt8 b = data[byte+2];        // delta components        UInt8 dr = abs(r-source255Components[0]);        UInt8 dg = abs(g-source255Components[1]);        UInt8 db = abs(b-source255Components[2]);        // ratio of 'how far away' each component is from the source color        CGfloat ratio = (dr+dg+db)/(255.0*3.0);        if (ratio > maxTolerance) ratio = 1; // if ratio is too far away,set it to max.        if (ratio < minTolerance) ratio = 0; // if ratio isn't far enough away,set it to min.        // blend color components        data[byte] = (UInt8)round(ratio*r)+(UInt8)round((1.0-ratio)*destination255Components[0]);        data[byte+1] = (UInt8)round(ratio*g)+(UInt8)round((1.0-ratio)*destination255Components[1]);        data[byte+2] = (UInt8)round(ratio*b)+(UInt8)round((1.0-ratio)*destination255Components[2]);    }    // get image from context    CGImageRef img = CGBitmapContextCreateImage(ctx);    // clean up    CGContextRelease(ctx);    CGcolorSpaceRelease(colorSpace);    free(data);    free(source255Components);    free(destination255Components);    UIImage* returnImage = [UIImage imageWithCGImage:img];    CGImageRelease(img);    return returnImage;}@end

用法:

UIImage* colaimage = [UIImage imagenamed:@"cola1.png"];UIImage* blackColaimage = [colaimage imageByReplacingcolor:[UIcolor colorWithRed:1 green:0 blue:0 Alpha:1] withMinTolerance:0.5 withMaxTolerance:0.6 withcolor:[UIcolor colorWithRed:0 green:0 blue:0 Alpha:1]];

minTolerance是像素开始与替换颜色混合(而不是被替换)的点. maxTolerance是像素停止混合的点.

之前:

后:

结果是有点混淆,但请记住,您的原始图像相当小.对于更高分辨率的图像,这将更好地工作.您还可以使用公差来获得更好的结果!

总结

以上是内存溢出为你收集整理的ios – 如何在不透明的UIImage上用另一种替换给定的颜色[复制]全部内容,希望文章能够帮你解决ios – 如何在不透明的UIImage上用另一种替换给定的颜色[复制]所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1009511.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存