CIImage翻转90度
ciImage = ciImage.oriented(CGImagePropertyOrientation.right)
UIImage翻转90度
UI层面的翻转,数据结构没变。
let flipImageOrientation = (detectionImage.imageOrientation.rawValue + 2) % 8
let flipImage = UIImage(cgImage:detectionImage.cgImage!,
scale:detectionImage.scale,
orientation:UIImage.Orientation(rawValue: flipImageOrientation)!)
UIImage旋转任意角度
extension UIImage
{
func rotate(by degrees: CGFloat, flip: Bool? = nil) -> UIImage
{
let radians = CGFloat(degrees * (CGFloat.pi / 180.0))
let bufferView = UIView(frame: CGRect(origin: CGPoint.zero, size: self.size))
let t: CGAffineTransform = CGAffineTransform(rotationAngle: radians)
bufferView.transform = t
let bufferSize = bufferView.frame.size
UIGraphicsBeginImageContextWithOptions(bufferSize, false, self.scale)
let bitmap = UIGraphicsGetCurrentContext()
bitmap?.translateBy(x: bufferSize.width / 2, y: bufferSize.height / 2)
bitmap?.rotate(by: radians)
if let isFlipped = flip {
if !isFlipped {
bitmap?.scaleBy(x: 1.0, y: -1.0)
} else {
bitmap?.scaleBy(x: -1.0, y: -1.0)
}
} else {
bitmap?.scaleBy(x: -1.0, y: -1.0)
}
bitmap?.draw(self.cgImage!, in: CGRect(origin: CGPoint(x: -self.size.width / 2, y: -self.size.height / 2), size: self.size))
let finalBuffer = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return finalBuffer!
}
}
UIImage矫正翻转
- (UIImage *)fixOrientation:(UIImage *)aImage {
// No-op if the orientation is already correct
if (aImage.imageOrientation == UIImageOrientationUp)
return aImage;
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;
switch (aImage.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch (aImage.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
default:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
CGImageGetBitsPerComponent(aImage.CGImage), 0,
CGImageGetColorSpace(aImage.CGImage),
CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch (aImage.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
break;
}
// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
NSImage旋转(90、180、270)
func orienteImage(degrees:CGFloat) -> NSImage{
if degrees != 90 && degrees != 180 && degrees != 270 {
return self
}
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)
var rotatedSize = self.size
if degrees == 90 || degrees == 270{
rotatedSize = CGSize(width: self.size.height,height: self.size.width)
}
let rotated = NSImage.init(size: rotatedSize)
transform = NSAffineTransform()
transform.translateX(by: rotatedSize.width/2, yBy: rotatedSize.height/2)
transform.rotate(byDegrees: degrees)
transform.translateX(by: -rotatedSize.width/2, yBy: -rotatedSize.height/2)
rotated.lockFocus()
transform.concat()
self.draw(in: imageBounds, from: NSZeroRect, operation: NSCompositingOperation.copy, fraction: 1.0)
rotated.unlockFocus()
return rotated
}