iOS/iOS Swift 개발 일기

[iOS swift] [의문점] UIGraphicsGetCurrentContext()

Developer-Michelle 2022. 5. 14. 17:47

 Core graphics이용해서 꽃 그리기 하다가 나온거 (do it 스위프트 책 p.612) : UIGraphicsGetCurrentContext()

 

왜 UIGraphicsBeginImageContext()를 쓰지 않고 UIGraphicsGetCurrentContext()를 쓰는가.. ?

UIGraphicsBeginImageContext() vs UIGraphicsGetCurrentContext() 차이점

 

 

https://developer.apple.com/documentation/uikit/1623918-uigraphicsgetcurrentcontext

 

Apple Developer Documentation

 

developer.apple.com

UIGraphicsGetCurrentContext()

Returns the current graphics context.
 

vs.

https://developer.apple.com/documentation/uikit/1623922-uigraphicsbeginimagecontext

 

Apple Developer Documentation

 

developer.apple.com

UIGraphicsBeginImageContext(_:)

 

 

UIGraphicsGetCurrentContext() 

현재 그래픽 컨텍스트에 대한 참조를 반환합니다. 생성하지 않습니다.

이러한 관점에서 보면 현재 컨텍스트가 그래픽 컨텍스트가 생성된 크기이기 때문에 크기 매개변수가 필요하지 않다는 것을 알 수 있기 때문에 이것은 기억하는 것이 중요합니다.

이 경우 그래픽 컨텍스트를 만들 필요가 없습니다. 

자동으로 생성되었으며 drawRect: 메소드에서 사용자 정의 그리기를 수행할 수 있습니다.

 

vs 


이제 다른 상황에서 drawRect: 메서드 외부에서 사용자 지정 그리기를 수행할 수 있습니다. 

여기서 UIGraphicsBeginImageContext(aSize)를 사용합니다.

UIGraphicsBeginImageContext(aSize) - 먼저 사이즈를 정의하고 시작.

UIView의 drawRect: 메소드 외부의 UIKit 레벨에서 그래픽 컨텍스트를 생성하기 위한 것입니다.
UIView의 하위 클래스가 있는 경우 다음과 같이 drawRect: 메서드를 재정의할 수 있습니다.