繁体   English   中英

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

[英]How to change the background of an UIImage in iphone to different colors

提前致谢。 我已经使用此代码将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文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM