2010-12-01 30 views
1

我添加了一个按钮到下方的工具栏是这样的:按钮图像的iPhone的UIBarButtonItem阿尔法

UIImage *locationImage = [UIImage imageNamed:@"193-location-arrow.png"]; 
UIBarButtonItem *locationButton = [[UIBarButtonItem alloc] initWithImage:locationImage style:UIBarButtonItemStyleBordered target:self action:@selector(updateCurrentLocation)]; 

NSArray *items = [[NSArray alloc] initWithObjects:locationButton,nil]; 
[toolbar setItems:items]; 
[items release]; 
[locationButton release]; 

这个伟大的工程,图像的Alpha是拿起精致,按钮显示是这样的:

Location Icon

于是,我把这个代码,并略作修改在我的导航栏创建一个按钮:

UIImage *favouriteImage = [UIImage imageNamed:@"28-star.png"]; 

UIBarButtonItem *favouriteButton = [[UIBarButtonItem alloc] initWithImage:favouriteImage style:UIBarButtonItemStyleBordered target:self action:nil]; 

self.navigationItem.rightBarButtonItem = favouriteButton; 

[favouriteButton release]; 

阿尔法似乎并不在这一个被拾起 - 它看起来是灰色的:

alt text

有什么我需要在导航栏使用自定义图像时设定?

感谢和问候,

丰富

回答

2

您可以将图像转换为白色的几行代码:

CGRect imageRect = CGRectMake(0, 0, inImage.size.width, inImage.size.height) 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

CGContextRef context = CGBitmapContextCreate(data1, imageRect.size.width, imageRect.size.height, 8, imageRect.size.width * 4, colorSpace, kCGImageAlphaPremultipliedLast); 
CGContextClipToMask(context, imageRect, inImage.CGImage); 
CGContextSetRGBFillColor(context1, 1, 1, 1, 1); 
CGContextFillRect(context, imageRect); 

CGImageRef finalImage = CGBitmapContextCreateImage(context); 
UIImage *returnImage = [UIImage imageWithCGImage:finalImage];  

CGContextRelease(context); 
CGColorSpaceRelease(colorSpace); 
CGImageRelease(finalImage); 
+0

感谢您的。但是,我想知道为什么这两个行为有所不同,但?只是为了我自己的好奇心!有任何想法吗? – Rich 2010-12-01 15:17:23