开发者

iPhone UIBarButtonItem alpha of button image

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:

iPhone UIBarButtonItem alpha of button image

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 relea开发者_开发技巧se];

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

iPhone UIBarButtonItem alpha of button image

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

Thanks and Regards,

Rich


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜