How to rotate an image 90 degrees on iOS?

后端 未结 14 1857
野的像风
野的像风 2020-11-29 00:55

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

相关标签:
14条回答
  • 2020-11-29 01:53

    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;
    
    0 讨论(0)
  • 2020-11-29 01:54

    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))
    
    0 讨论(0)
提交回复
热议问题