mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 17:08:39 +00:00
rotate function for images
This commit is contained in:
parent
0956b4ebcd
commit
9d343a31d3
@ -49,4 +49,35 @@ extension NSImage {
|
||||
// Return the new image
|
||||
return imageWithNewSize
|
||||
}
|
||||
|
||||
func rotateByDegreess(degrees:CGFloat) -> NSImage {
|
||||
|
||||
var imageBounds = NSZeroRect ; imageBounds.size = self.size
|
||||
let pathBounds = NSBezierPath(rect: imageBounds)
|
||||
var transform = NSAffineTransform()
|
||||
transform.rotate(byDegrees: degrees)
|
||||
pathBounds.transform(using: transform as AffineTransform)
|
||||
let rotatedBounds:NSRect = NSMakeRect(NSZeroPoint.x, NSZeroPoint.y , self.size.width, self.size.height )
|
||||
let rotatedImage = NSImage(size: rotatedBounds.size)
|
||||
|
||||
//Center the image within the rotated bounds
|
||||
imageBounds.origin.x = NSMidX(rotatedBounds) - (NSWidth(imageBounds) / 2)
|
||||
imageBounds.origin.y = NSMidY(rotatedBounds) - (NSHeight(imageBounds) / 2)
|
||||
|
||||
// Start a new transform
|
||||
transform = NSAffineTransform()
|
||||
// Move coordinate system to the center (since we want to rotate around the center)
|
||||
transform.translateX(by: +(NSWidth(rotatedBounds) / 2 ), yBy: +(NSHeight(rotatedBounds) / 2))
|
||||
transform.rotate(byDegrees: degrees)
|
||||
// Move the coordinate system bak to normal
|
||||
transform.translateX(by: -(NSWidth(rotatedBounds) / 2 ), yBy: -(NSHeight(rotatedBounds) / 2))
|
||||
// Draw the original image, rotated, into the new image
|
||||
rotatedImage.lockFocus()
|
||||
transform.concat()
|
||||
self.draw(in: imageBounds, from: NSZeroRect, operation: NSCompositingOperation.copy, fraction: 1.0)
|
||||
rotatedImage.unlockFocus()
|
||||
|
||||
return rotatedImage
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user