[iOS] Image Rendering Mode

728x90
반응형

 

이미지 렌더링 모드에 대해 알아 보도록 하겠습니다.

 

UIImage의 RenderingMode에는 아래와 같이 3가지의 옵션이 있습니다.

extension UIImage {
@available(iOS 7.0, *)
public enum RenderingMode : Int {
case automatic // Use the default rendering mode for the context where the image is used
case alwaysOriginal // Always draw the original image, without treating it as a template
case alwaysTemplate // Always draw the image as a template image, ignoring its color information
}
}

이미지 렌더링이란 무엇일까요?

개발을 하다 보면 분명 이미지를 넣었는데 파란색으로 나오는 경우를 보셨을 겁니다.

etc-image-0

 

 

1. automaic

이미지렌더링모드 중에서 automatic은 default입니다.

뷰 상에선 제대로 나오는데 탭바에서 이미지가 불투명한 부분을 틴트 컬러로 보이고 있습니다.

let image = UIImage(named: "cart")?.withRenderingMode(.automatic) // Default

 

 

2. alwaysTemplate

원본 이미지에서 컬러 정보가 모두 제거되고, 불투명한 부분을 틴트 컬러로 보이게됩니다.

let image = UIImage(named: "cart")?.withRenderingMode(.alwaysTemplate)

스크린샷 2019-08-01 오전 12.09.48.png

 

3. alwaysOriginals

원본(Originals) 이미지에서 컬러 정보가 모두 보이게 됩니다.

let image = UIImage(named: "cart")?.withRenderingMode(.alwaysOriginal)

스크린샷 2019-08-01 오전 12.14.20.png

 

 

 

코드가 아닌 Xcode에서 이미지의 렌더링 변경하는 방법

 

  • Assets -> 이미지 선택 후 Render As

스크린샷 2019-08-01 오전 12.16.55.png
스크린샷 2019-08-01 오전 12.17.01.png

 

반응형