如何使一个颜色透明的UIImage? [英] How to make one color transparent on a UIImage?

查看:140
本文介绍了如何使一个颜色透明的UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序,我有一个UIImage实例。我想得到一个衍生的UIImage,它是第一个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 * contributedimage = [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;
}

上面的代码被测试,我改变绿色为红色面具


On my iPhone app I have a UIImage instance. I want to get a derived a UIImage that is the result of the first UIImage where one of its colors (e.g. magenta) is made transparent. How can I do this?

解决方案

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

The above code is tested and I changed the green color to red color by using mask

这篇关于如何使一个颜色透明的UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆