2011-05-18 22 views
3

在此先感謝。 我已經使用此代碼將UIImage的背景更改爲redcolor。如何在iphone中將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};,但它仍然是紅色。我的圖像背景是黑色的。任何人都可以幫助我做到這一點。

回答

0

你確定你正在掩蓋你的形象嗎?而不是顯示模板圖像只顯示設備上的面具,我想你會發現,面具是負責你的紅顏色,CGBitmapContextCreate看起來不正確的PNG文件不斷

kCGImageAlphaPremultipliedFirst 

應該(我相信)是

kCGImageAlphaPremultipliedLast 

爲png文件

+0

感謝UR響應。不,我曾嘗試使用kCGImageAlphaPremultipliedLast解決png文件,當時它不會生效。 – 2011-05-19 07:30:04

相關問題