iPhone UIBarButtonItem按钮图像的alpha

时间:2010-12-01 13:23:27

标签: iphone image uibarbuttonitem alpha

我在下方工具栏中添加了一个按钮,如下所示:

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

在导航栏中使用自定义图像时是否需要设置一些内容?

谢谢和问候,

1 个答案:

答案 0 :(得分: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);