iOS/이슈
[iOS Issue] CALayer의 contents의 image가 안보임
Sky Titan
2022. 6. 25. 20:09
728x90
CALayer의 contents의 image가 안보임
CALayer's image not showing up
I am trying to use CALayer to display an image, but the image is not showing up. I've verified that 'image' is not nil, image.size is correct, and layer.contents is a valid CABackingStore. - (void)
stackoverflow.com
이슈라기 보단 내가 뭔가 CALayer의 원리 중 놓친 부분이 있어서 일어난 현상 같은데, 결과적으론 CALayer의 contents 사용 시 setNeedsDisplay()를 호출해서 image가 안 보였던 현상이다.
setNeedsDisplay()를 지워주니까 바로 해결;;
해석해보면, setNeedsDisplay()를 호출하는 순간 원래 존재하던 contents는 새로운 content를 만들기 위해 다 지워지기 때문이라는 것.
let ringImageLayer = CALayer()
ringImageLayer.frame = CGRect(x: 0, y: 0, width: self.bounds.width - ringLineWidth, height: self.bounds.height - ringLineWidth)
ringImageLayer.contentsScale = self.contentsScale
ringImageLayer.contents = ringImage.cgImage
ringImageLayer.contentsGravity = .center
self.addSublayer(ringImageLayer)728x90