iPhone UIBarButtonItem 按钮图像的 alpha

发布于 2024-10-05 05:29:21 字数 1090 浏览 1 评论 0原文

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

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];

alpha 似乎没有在这个按钮上被拾取 - 它看起来是灰色的:

alt text

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

谢谢和问候,

丰富

I've added a button to a lower toolbar like this:

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];

This works great, the alpha of the image is picked up fine, the button displays like this:

Location Icon

So, I took this code and modified slightly to create a button in my navigation bar:

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];

The alpha doesn't seem to be picked up on this one - it looks greyed out:

alt text

Is there something I need to set when using custom images in the navigation bar?

Thanks and Regards,

Rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生路 2024-10-12 05:29:21

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

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);

You could convert the image to white with a few lines of code:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文