extension UIImage {
func fixImageOrientation() -> UIImage? {
var flip:Bool = false //used to see if the image is mirrored
var isRotatedBy90:Bool = false // used to check whether aspect ratio is to be changed or not
var transform = CGAffineTransform.identity
//check current orientation of original image
switch self.imageOrientation {
case .down, .downMirrored:
transform = transform.rotated(by: CGFloat(M_PI));
case .left, .leftMirrored:
transform = transform.rotated(by: CGFloat(M_PI_2));
isRotatedBy90 = true
case .right, .rightMirrored:
transform = transform.rotated(by: CGFloat(-M_PI_2));
isRotatedBy90 = true
case .up, .upMirrored:
break
}
switch self.imageOrientation {
case .upMirrored, .downMirrored:
transform = transform.translatedBy(x: self.size.width, y: 0)
flip = true
case .leftMirrored, .rightMirrored:
transform = transform.translatedBy(x: self.size.height, y: 0)
flip = true
default:
break;
}
// calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox = UIView(frame: CGRect(origin: CGPoint(x:0, y:0), size: size))
rotatedViewBox.transform = transform
let rotatedSize = rotatedViewBox.frame.size
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap = UIGraphicsGetCurrentContext()
// Move the origin to the middle of the image so we will rotate and scale around the center.
bitmap!.translateBy(x: rotatedSize.width / 2.0, y: rotatedSize.height / 2.0);
// Now, draw the rotated/scaled image into the context
var yFlip: CGFloat
if(flip){
yFlip = CGFloat(-1.0)
} else {
yFlip = CGFloat(1.0)
}
bitmap!.scaleBy(x: yFlip, y: -1.0)
//check if we have to fix the aspect ratio
if isRotatedBy90 {
bitmap?.draw(self.cgImage!, in: CGRect(x: -size.width / 2, y: -size.height / 2, width: size.height,height: size.width))
} else {
bitmap?.draw(self.cgImage!, in: CGRect(x: -size.width / 2, y: -size.height / 2, width: size.width,height: size.height))
}
let fixedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return fixedImage
}
2017年3月26日 星期日
how-to-rotate-image-in-swift-3
Ref : http://stackoverflow.com/questions/40882487/how-to-rotate-image-in-swift-3
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言