首页 > 代码库 > iOS 为导航栏自定义按钮图案Button Image 运行出来的颜色与原本颜色不一样 -解决方案

iOS 为导航栏自定义按钮图案Button Image 运行出来的颜色与原本颜色不一样 -解决方案

为相机制作闪光灯,在导航栏自定义了“闪光”图案,希望点击时变换图片,但是一直没有改变,原来是因为设置了Global Tint的颜色,所以系统会自动把图片的颜色改为Global Tint的颜色。

解决方案,设置图片时,添加:imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal

源码:

- (void) setFlashOn:(BOOL)isOn
{
    if (self.captureDevice.hasFlash) {
        UIImage* flashlIcon;
        [self.captureDevice lockForConfiguration:nil]; //you must lock before setting torch mode
        if (isOn) {
            NSLog(@"set flash on");
            [self.captureDevice setFlashMode:AVCaptureFlashModeOn];
            flashlIcon = [UIImage imageNamed:@"flash_on"];
        }
        else{
            NSLog(@"set flash off");
            [self.captureDevice setFlashMode:AVCaptureFlashModeOff];
            flashlIcon = [UIImage imageNamed:@"flash_off"];
        }
        [self.captureDevice unlockForConfiguration];
        [self.navigationItem.rightBarButtonItem setImage:[flashlIcon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    }
    else{
        NSLog(@"Sorry, this device doesn't have flash.");
    }
}


参考:

http://stackoverflow.com/questions/21252194/navigation-bar-button-item-image-color-is-different-when-design-through-xib-of-x