gpt4 book ai didi

iphone - 如何将iPhone中的UIImage的背景更改为不同的颜色

转载 作者:行者123 更新时间:2023-12-03 20:16:11 24 4
gpt4 key购买 nike

提前致谢。 我使用此代码将 UIImage 的背景更改为红色。

-(void)changeColor
{
UIImage *temp23=[UIImage imageNamed:@"leaf.png"];
CGImageRef ref1=[self createMask:temp23];
const float colorMasking[6] = {1.0, 2.0, 1.0, 1.0, 1.0, 1.0};
CGImageRef New=CGImageCreateWithMaskingColors(ref1, colorMasking);
UIImage *resultedimage=[UIImage imageWithCGImage:New];
}

-(CGImageRef)createMask:(UIImage*)temp
{
CGImageRef ref=temp.CGImage;
int mWidth=CGImageGetWidth(ref);
int mHeight=CGImageGetHeight(ref);
int count=mWidth*mHeight*4;
void *bufferdata=malloc(count);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGContextRef cgctx = CGBitmapContextCreate (bufferdata,mWidth,mHeight, 8,mWidth*4, colorSpaceRef, kCGImageAlphaPremultipliedFirst);

CGRect rect = {0,0,mWidth,mHeight};
CGContextDrawImage(cgctx, rect, ref);
bufferdata = CGBitmapContextGetData (cgctx);

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bufferdata, mWidth*mHeight*4, NULL);
CGImageRef savedimageref = CGImageCreate(mWidth,mHeight, 8, 32, mWidth*4, colorSpaceRef, bitmapInfo,provider , NULL, NO, renderingIntent);
CFRelease(colorSpaceRef);
return savedimageref;
}

但是如果我尝试更改为不同的颜色,我将无法得到我需要更改的内容。我在这里尝试使用不同的值 const float colorMasking[6] = {1.0, 2.0, 1.0, 1.0, 1.0, 1.0}; 但它仍然是红色。我的图像背景是黑色的。任何人都可以帮我做到这一点吗?

最佳答案

您确定正确遮盖了您的图像吗?不显示蒙版图像,只需在您的设备上显示蒙版,我想您会发现蒙版负责您的红色,CGBitmapContextCreate 对于 png 文件常量看起来不正确

kCGImageAlphaPremultipliedFirst

应该(我相信)是

kCGImageAlphaPremultipliedLast

对于 png 文件

关于iphone - 如何将iPhone中的UIImage的背景更改为不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6042494/

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