[swift] Swift Core Graphics를 사용하여 이미지에 색조 적용하기

이번에는 Swift에서 Core Graphics를 사용하여 이미지에 색조를 적용하는 방법에 대해 알아보겠습니다.

1. 이미지 가져오기

먼저, 다음과 같이 이미지를 가져옵니다.

import UIKit

let image = UIImage(named: "example_image")

2. Core Graphics를 사용하여 이미지에 색조 적용하기

다음으로, Core Graphics를 사용하여 이미지에 색조를 적용합니다.

extension UIImage {
    func applyTint(color: UIColor) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale)
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        defer { UIGraphicsEndImageContext() }
        
        color.set()
        context.translateBy(x: 0, y: size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.setBlendMode(.color)
        
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        context.draw(self.cgImage!, in: rect)
        context.clip(to: rect, mask: self.cgImage!)
        context.fill(rect)
        
        guard let coloredImage = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
        return coloredImage
    }
}

let tintedImage = image?.applyTint(color: UIColor.red)

이제 tintedImage에는 원하는 색조가 적용된 이미지가 포함되어 있습니다.

위 방법을 통해 Swift에서 Core Graphics를 사용하여 이미지에 색조를 적용할 수 있습니다. 필요에 따라 원하는 색조를 적용하여 이미지를 개성화할 수 있습니다.

이상으로 Swift Core Graphics를 사용하여 이미지에 색조를 적용하는 방법에 대해 알아보았습니다. 만약 질문이 있으시다면 언제든지 물어보세요!