开发者

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

开发者 https://www.devze.com 2023-03-07 03:41 出处:网络
Thanks in advance. I have used this code to change the background of an UIImage into redcolor. -(void)changeColor

Thanks in advance. I have used this code to change the background of an UIImage into redcolor.

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

But if i try to change into different color i am not getting what i need to change. I tried with different values here const float colorMasking[6] = {1.0, 2.0, 1.0, 1.0, 1.0, 1.0}; but it was still in red color . my image background is black. Can any one help me to do this.


Are you sure you are masking your image properly ? instead of displaying the masked image just display the mask on your device and i think youll find that the mask is responsible for your red colour, the CGBitmapContextCreate looks incorrect for a png file the constant

kCGImageAlphaPremultipliedFirst

should (i believe) be

kCGImageAlphaPremultipliedLast

for a png file

0

精彩评论

暂无评论...
验证码 换一张
取 消