【问题标题】:iPhone UIBarButtonItem alpha of button image按钮图像的 iPhone UIBarButtonItem alpha
【发布时间】:2010-12-01 13:23:27
【问题描述】:

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

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 拾取得很好,按钮显示如下:

因此,我采用了这段代码并稍作修改,在导航栏中创建了一个按钮:

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 似乎没有被选中 - 它看起来是灰色的:

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

感谢和问候,

丰富

【问题讨论】:

    标签: iphone image uibarbuttonitem alpha


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。但是,我想知道为什么两者的行为不同?只是为了我自己的好奇心!有什么想法吗?
    • UIBarButtonItem 在技术上不是 UIView 子类,它只是一个数据结构 - UIBarButtonItems 的实际呈现取决于显示它们的工具栏/其他视图。不是 100% 确定为什么 Apple 选择应用它UIToolbars 中的按钮中的白化过滤器,而不是 UINavigationBars,但我在这里看到了同样的问题,所以你没有想象到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多