有没有一种在Mac应用程序中旋转NSImage的简单方法?还是用Swift设置从肖像到景观的方向?
我在玩CATransform3DMakeAffineTransform,但我不能让它工作。
CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation(CGFloat(M_PI) * 90/180))
这是我第一次使用转换。所以请对我耐心点:)也许我在用错误的方法.
有人能帮我吗?
谢谢!
发布于 2019-04-03 15:12:44
下面是一个简单的Swift (4+)解决方案,用于绘制围绕中心旋转的图像:
extension NSImage {
/// Rotates the image by the specified degrees around the center.
/// Note that if the angle is not a multiple of 90°, parts of the rotated image may be drawn outside the image bounds.
func rotated(by angle: CGFloat) -> NSImage {
let img = NSImage(size: self.size, flipped: false, drawingHandler: { (rect) -> Bool in
let (width, height) = (rect.size.width, rect.size.height)
let transform = NSAffineTransform()
transform.translateX(by: width / 2, yBy: height / 2)
transform.rotate(byDegrees: angle)
transform.translateX(by: -width / 2, yBy: -height / 2)
transform.concat()
self.draw(in: rect)
return true
})
img.isTemplate = self.isTemplate // preserve the underlying image's template setting
return img
}
}
https://stackoverflow.com/questions/31699235
复制相似问题