[swift] Core Image를 사용하여 이미지를 회전하고 확대하는 방법은?
import CoreImage
import UIKit
func applyAffineTransform(to image: UIImage, with angle: CGFloat, and scale: CGFloat) -> UIImage? {
guard let cgImage = image.cgImage else { return nil }
let ciImage = CIImage(cgImage: cgImage)
let transformation = CGAffineTransform(translationX: ciImage.extent.width / 2, y: ciImage.extent.height / 2)
.rotated(by: angle)
.scaledBy(x: scale, y: scale)
.translatedBy(x: -ciImage.extent.width / 2, y: -ciImage.extent.height / 2)
let transformedImage = ciImage.transformed(by: transformation)
let context = CIContext(options: nil)
if let outputCGImage = context.createCGImage(transformedImage, from: transformedImage.extent) {
return UIImage(cgImage: outputCGImage)
}
return nil
}
let inputImage = UIImage(named: "inputImage.jpg")!
if let outputImage = applyAffineTransform(to: inputImage, with: .pi/4, and: 1.5) {
// 사용할 수 있는 회전된 이미지(outputImage)
}
위 코드를 사용하면 inputImage
를 .pi/4
만큼 회전하고 1.5배 확대된 outputImage
를 얻을 수 있습니다.