Rotating a CGImage
I got your problem.You have to translate back the context using CGContextTranslateCTM(context, -rotatedSize.width/2, -rotatedSize.height/2);
as well as set origin of rect in drawing to rotatedViewBox.frame.origin.x,rotatedViewBox.frame.origin.y.
use this code.
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(0.3);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, rotatedSize.width/2, rotatedSize.height/2);
CGContextRotateCTM (context, 0.3);
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, -rotatedSize.width/2, -rotatedSize.height/2);
CGContextDrawImage(context, CGRectMake(-rotatedViewBox.frame.origin.x, -rotatedViewBox.frame.origin.y, image.size.width, image.size.height), [image CGImage]);
UIImage *nn = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"size is %f, %f",nn.size.width,nn.size.height);
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(nn, nil,nil,nil);
The Swift 3 version for the Cameron Lowell Palmer answer:
func createMatchingBackingDataWithImage(imageRef: CGImage?, orienation: UIImageOrientation) -> CGImage? {
var orientedImage: CGImage?
if let imageRef = imageRef {
let originalWidth = imageRef.width
let originalHeight = imageRef.height
let bitsPerComponent = imageRef.bitsPerComponent
let bytesPerRow = imageRef.bytesPerRow
let colorSpace = imageRef.colorSpace
let bitmapInfo = imageRef.bitmapInfo
var degreesToRotate: Double
var swapWidthHeight: Bool
var mirrored: Bool
switch orienation {
case .up:
degreesToRotate = 0.0
swapWidthHeight = false
mirrored = false
break
case .upMirrored:
degreesToRotate = 0.0
swapWidthHeight = false
mirrored = true
break
case .right:
degreesToRotate = 90.0
swapWidthHeight = true
mirrored = false
break
case .rightMirrored:
degreesToRotate = 90.0
swapWidthHeight = true
mirrored = true
break
case .down:
degreesToRotate = 180.0
swapWidthHeight = false
mirrored = false
break
case .downMirrored:
degreesToRotate = 180.0
swapWidthHeight = false
mirrored = true
break
case .left:
degreesToRotate = -90.0
swapWidthHeight = true
mirrored = false
break
case .leftMirrored:
degreesToRotate = -90.0
swapWidthHeight = true
mirrored = true
break
}
let radians = degreesToRotate * Double.pi / 180
var width: Int
var height: Int
if swapWidthHeight {
width = originalHeight
height = originalWidth
} else {
width = originalWidth
height = originalHeight
}
if let contextRef = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace!, bitmapInfo: bitmapInfo.rawValue) {
contextRef.translateBy(x: CGFloat(width) / 2.0, y: CGFloat(height) / 2.0)
if mirrored {
contextRef.scaleBy(x: -1.0, y: 1.0)
}
contextRef.rotate(by: CGFloat(radians))
if swapWidthHeight {
contextRef.translateBy(x: -CGFloat(height) / 2.0, y: -CGFloat(width) / 2.0)
} else {
contextRef.translateBy(x: -CGFloat(width) / 2.0, y: -CGFloat(height) / 2.0)
}
contextRef.draw(imageRef, in: CGRect(x: 0, y: 0, width: originalWidth, height: originalHeight))
orientedImage = contextRef.makeImage()
}
}
return orientedImage
}
Usage:
let newCgImage = createMatchingBackingDataWithImage(imageRef: cgImageRef, orienation: .left)
-(UIImage*) rotate:(UIImage*) src andOrientation:(UIImageOrientation)orientation
{
UIGraphicsBeginImageContext(src.size);
CGContextRef context=(UIGraphicsGetCurrentContext());
if (orientation == UIImageOrientationRight) {
CGContextRotateCTM (context, 90/180*M_PI) ;
} else if (orientation == UIImageOrientationLeft) {
CGContextRotateCTM (context, -90/180*M_PI);
} else if (orientation == UIImageOrientationDown) {
// NOTHING
} else if (orientation == UIImageOrientationUp) {
CGContextRotateCTM (context, 90/180*M_PI);
}
[src drawAtPoint:CGPointMake(0, 0)];
UIImage *img=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
use this one .. it works perfect ! Just make sure you use this function when you are picking images from UIImagePicker:
example:
UIImage *image;
image = [self rotate:image andOrientation:image.imageOrientation];
Make sure to use this function in place where you pick photos from documents directory.
Rotate and Mirror UIImage CGImage Backing Data - Swift
I recently wanted to correct a UIImage
backed by a CGImage
to match the desired orientation rather than rely on APIs to respect the UIImageOrientation parameter.
func createMatchingBackingDataWithImage(imageRef: CGImage?, orienation: UIImageOrientation) -> CGImage?
{
var orientedImage: CGImage?
if let imageRef = imageRef {
let originalWidth = imageRef.width
let originalHeight = imageRef.height
let bitsPerComponent = imageRef.bitsPerComponent
let bytesPerRow = imageRef.bytesPerRow
let bitmapInfo = imageRef.bitmapInfo
guard let colorSpace = imageRef.colorSpace else {
return nil
}
var degreesToRotate: Double
var swapWidthHeight: Bool
var mirrored: Bool
switch orienation {
case .up:
degreesToRotate = 0.0
swapWidthHeight = false
mirrored = false
break
case .upMirrored:
degreesToRotate = 0.0
swapWidthHeight = false
mirrored = true
break
case .right:
degreesToRotate = 90.0
swapWidthHeight = true
mirrored = false
break
case .rightMirrored:
degreesToRotate = 90.0
swapWidthHeight = true
mirrored = true
break
case .down:
degreesToRotate = 180.0
swapWidthHeight = false
mirrored = false
break
case .downMirrored:
degreesToRotate = 180.0
swapWidthHeight = false
mirrored = true
break
case .left:
degreesToRotate = -90.0
swapWidthHeight = true
mirrored = false
break
case .leftMirrored:
degreesToRotate = -90.0
swapWidthHeight = true
mirrored = true
break
}
let radians = degreesToRotate * Double.pi / 180.0
var width: Int
var height: Int
if swapWidthHeight {
width = originalHeight
height = originalWidth
} else {
width = originalWidth
height = originalHeight
}
let contextRef = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
contextRef?.translateBy(x: CGFloat(width) / 2.0, y: CGFloat(height) / 2.0)
if mirrored {
contextRef?.scaleBy(x: -1.0, y: 1.0)
}
contextRef?.rotate(by: CGFloat(radians))
if swapWidthHeight {
contextRef?.translateBy(x: -CGFloat(height) / 2.0, y: -CGFloat(width) / 2.0)
} else {
contextRef?.translateBy(x: -CGFloat(width) / 2.0, y: -CGFloat(height) / 2.0)
}
contextRef?.draw(imageRef, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(originalWidth), height: CGFloat(originalHeight)))
orientedImage = contextRef?.makeImage()
}
return orientedImage
}