2010-12-01 61 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