2017-09-25 4 views
0

디자이너가 나에게 준 이미지의 색상과 불투명도를 동적으로 변경하려고합니다. 물론 다음 코드와 함께 원활하게 작동합니다.ios UIImageRenderingModeAlwaysTemplate이 불투명도를 무시하지 않습니다.

_imgViewForMenu.tintColor = [_lblForMenu.textColor colorWithAlphaComponent:1.0f]; 
// This alpha component wont affect the png image with 38% opacity. 
// You will never get full black image with [UIColor blackColor] 
// and alpha component 1.0 

_imgViewForMenu.image = [imageForMenuIcon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 

이미지에 불투명도가없는 경우에만 작동합니다. 코드에 대한 주석에서 말한 것 외에는 작동하지 않을 것입니다.

그래서 문제는 이미지에서 색상 구성 요소와 불투명도를 모두 무시하도록 렌더링하는 것입니다. UITabBar 및 UIBarButonItem과 같은 시스템 컨트롤은 쉽게 수행 할 수 있습니다. UIImageView를 사용하면 어떨까요?

답변

0

이 시도 :

extension UIImage { 
    func tinted(with color: UIColor) -> UIImage? { 
     let image = withRenderingMode(.alwaysTemplate) 
     UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale) 
     color.set() 
     image.draw(in: CGRect(x: 0.0, y: 0.0, width: image.size.width, height: image.size.height)) 
     let tintedImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return tintedImage 
    } 
} 
관련 문제