๋””์ž์ธ ๊ฐ€์ด๋“œ๋ฅผ ์ฝ๋‹ค๋ณด๋ฉด, Asset์ด ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ ๊ทธ๋ƒฅ View๋ฅผ ๋งŒ๋“ค๊ณ  Imageํ™” ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๊ฒ ๋Š”๋ฐ.. ์ž˜ ์ฐพ์•„์™”๋‹ค!!

Answer

extension UIView {
 
    public func asImage() -> UIImage {
        UIGraphicsImageRenderer(bounds: self.bounds).image {
            self.layer.render(in: $0.cgContext)
        }
    }
 
}

UIGraphicsImageRenderer

๊ทธ๋Ÿฐ๋ฐ ์ด ์นœ๊ตฌ๊ฐ€ ๋ญํ•˜๋Š” ๋…€์„์ธ์ง€ ๋ฌธ๋“ ๊ถ๊ธˆํ•ด์กŒ๋‹ค.

Core Graphics ์ง€์› ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๊ทธ๋ž˜ํ”ฝ ๋ Œ๋”๋Ÿฌ

UIKit ๊ธฐ๋ฐ˜ ์•ฑ์„ ์œ„ํ•ด Core Graphics์— ๋Œ€ํ•œ ๊ฒŒ์ดํŠธ์›จ์ด ์—ญํ• ์„ ํ•œ๋‹ค. ์ฆ‰, renderer ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ๊ฐ์ฒด ๋‚ด์— rendering context๋ฅผ ์ž‘์„ฑํ•˜๋ฉด, Core Graphics์—์„œ ํ•ด๋‹น ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜์—ฌ ๋‹ค์‹œ UIKit์— ํ•ด๋‹นํ•˜๋Š” ๋…€์„์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค. ๊ณต์‹ ๋ฌธ์„œ์— ์žˆ๋Š” ์˜ˆ์‹œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋ฉด, ์–ด๋–ค ์นœ๊ตฌ์ธ์ง€ ๋Œ€๋ฒˆ์— ์•Œ ์ˆ˜ ์žˆ๋‹ค.

let renderer = UIGraphicsImageRenderer(size: CGSize(width: 200, height: 200)) // Format์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ํ•ด๋‹น initilizer๋ฅผ ์‚ฌ์šฉ
 
// UIImage๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์„œ๋“œ. jpg, png๋„ ์žˆ๋‹ค.
let image = renderer.image { (context) in
  UIColor.darkGray.setStroke()
  context.stroke(renderer.format.bounds)
  UIColor(colorLiteralRed: 158/255, green: 215/255, blue: 245/255, alpha: 1).setFill()
  context.fill(CGRect(x: 1, y: 1, width: 140, height: 140))
  UIColor(colorLiteralRed: 145/255, green: 211/255, blue: 205/255, alpha: 1).setFill()
  context.fill(CGRect(x: 60, y: 60, width: 140, height: 140), blendMode: .multiply)
  
  UIColor(colorLiteralRed: 203/255, green: 222/255, blue: 116/255, alpha: 0.6).setFill()
  context.cgContext.fillEllipse(in: CGRect(x: 60, y: 60, width: 140, height: 140)) // CGContext ์‚ฌ์šฉ
}
  1. ํฌ๊ธฐ๊ฐ€ 200x200์ธ UIGraphicsImageRenderer ๊ฐœ์ฒด renderer๋ฅผ ๋งŒ๋“ ๋‹ค.
    • renderer๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ๋ Œ๋”๋ง์ด ์‹คํ–‰๋˜์ง€๋Š” ์•Š๋Š”๋‹ค. renderer์˜ image() ๋ฉ”์„œ๋“œ์—์„œ ์‹คํ–‰๋œ๋‹ค.
  2. renderer.image์˜ closure๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
    • func image(actions: (UIGraphicsImageRendererContext) -> Void) -> UIImage
    • ํด๋กœ์ €๋งŒ์„ ์ž…๋ ฅ ์ธ์ˆ˜๋กœ ๋ฐ›๊ณ , ๊ฒฐ๊ณผ๋กœ UIImage๋ฅผ ์ค€๋‹ค.
    • closure ๋‚ด๋ถ€์—๋Š” UIGraphicsImageRenderContext๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด๋…€์„์€ UIGraphicsRendeerContext๋ฅผ ์ƒ์†ํ•˜๊ณ  ์žˆ์–ด, ๊ทธ๋ฆฌ๊ธฐ์— ๊ด€๋ จ๋œ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” cgContext๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๋…€์„์„ ์‚ฌ์šฉํ•ด์„œ ๊ทธ๋ ค์ฃผ๋ฉด ๋œ๋‹ค.
    • UIGraphicsImageRenderContext์—๋„ ๊ธฐ๋ณธ์ ์ธ ๊ทธ๋ฆฌ๊ธฐ ๋ฉ”์„œ๋“œ๋Š” ์žˆ์ง€๋งŒ, ๊ณ ๊ธ‰ ๋™์ž‘์„ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด UIGraphicsRendeerContext.CGContext๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์ด ์ž‘์—…์„ ๋งˆ์น˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋„ํ˜•์ด ๊ทธ๋ ค์ง„๋‹ค.

์š”์•ฝ

  • UIGraphicsImageRendererContext๋Š” Core Graphic ๋ Œ๋”๋ง ๊ธฐ๋Šฅ์„ ์ง์ ‘์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” cgContext๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
  • ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— UIKit์—์„œ Core Graphic์— ์ ‘๊ทผํ•˜์—ฌ ์ด๋ฏธ์ง€ ๋ Œ๋”๋ง์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋Ÿฐ ์˜๋ฏธ์—์„œ ์• ํ”Œ์˜ ์ •์˜๋Š” ์–ด๋Š์ •๋„ ์ดํ•ด๊ฐ€ ๋˜๋Š” ๋ถ€๋ถ„์ด๋‹ค.

๋!

Reference