What I want to do is take a snapshot from my camera , send it to a server and then the server sends me back the image on a viewController. If the image is in portrait mode t
It´s best only use the bitmap. In my case use a UIImage that is in reference.image and I converted image at base64String. Without CGAffineTransformMakeRotation and without rotatedViewBox.transform
var rotatedSize = reference.image.size;
//Create bitmap context
UIGraphicsBeginImageContext(rotatedSize);
var bitmap = UIGraphicsGetCurrentContext();
//move origin to the middle of the image for rotation
CGContextTranslateCTM(bitmap, rotatedSize.width / 2, rotatedSize.height / 2);
//rotate image context
CGContextRotateCTM(bitmap, 1.5708);
CGContextDrawImage(bitmap, CGRectMake(-reference.image.size.width / 2, -reference.image.size.height / 2, reference.image.size.width, reference.image.size.height), reference.image.CGImage);
var rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
var base64Image = UIImageJPEGRepresentation(rotatedImage, compression).base64EncodedStringWithOptions(0);
UIGraphicsEndImageContext();
return base64Image;
Swift 4 | Xcode 9 syntax (please note that this code rotates a UIImageView 90 degrees clockwise, not a UIImage):
myImageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))