回答
您可以旋转UIImageView
本身:
UIImageView *iv = [[UIImageView alloc] initWithImage:image];
iv.transform = CGAffineTransformMakeRotation(M_PI_2);
或者,如果你真的想改变形象,你可以使用代码this answer,它的工作原理。
还有imageWithCIImage:scale:orientation
如果你想旋转UIImage
不是UIImageView
与这些方向之一:
typedef enum {
UIImageOrientationUp,
UIImageOrientationDown, // 180 deg rotation
UIImageOrientationLeft, // 90 deg CW
UIImageOrientationRight, // 90 deg CCW
UIImageOrientationUpMirrored, // vertical flip
UIImageOrientationDownMirrored, // horizontal flip
UIImageOrientationLeftMirrored, // 90 deg CW then perform horizontal flip
UIImageOrientationRightMirrored, // 90 deg CCW then perform vertical flip
} UIImageOrientation;
要旋转的像素可以使用以下。这创建了一个具有旋转元数据的中间UIImage,并将其呈现为宽高比尺寸调换的图像上下文。将得到的图像具有旋转(即基本CGImage)的像素
- (UIImage*)rotateUIImage:(UIImage*)sourceImage clockwise:(BOOL)clockwise
{
CGSize size = sourceImage.size;
UIGraphicsBeginImageContext(CGSizeMake(size.height, size.width));
[[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:clockwise ? UIImageOrientationRight : UIImageOrientationLeft] drawInRect:CGRectMake(0,0,size.height ,size.width)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
有可以传递为取向参数,实现180度旋转的其它可能的值和翻转等
如果原始图像是'UIImageOrientationRight',并且您在此处设置了'UIImageOrientationLeft',那么生成的输出将被压扁以适应新的尺寸。 – ray 2016-04-29 17:26:36
UIImage *img = [UIImage [email protected]"aaa.png"];
UIImage *image = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationRight];
随着斯威夫特,你可以通过做一个图像旋转:
var image: UIImage = UIImage(named: "headerBack.png")
var imageRotated: UIImage = UIImage(CGImage: image.CGImage, scale:1, orientation: UIImageOrientation.UpMirrored)
这将旋转任何给定的度数的图像。
注意这个作品2倍和3倍视网膜以及
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees {
CGFloat radians = DegreesToRadians(degrees);
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.size.width, self.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(radians);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, [[UIScreen mainScreen] scale]);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
CGContextRotateCTM(bitmap, radians);
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-self.size.width/2, -self.size.height/2 , self.size.width, self.size.height), self.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
这里是@迅速版RyanG的Objective C代码的一个扩展的UIImage:
extension UIImage {
func rotate(byDegrees degree: Double) -> UIImage {
let radians = CGFloat(degree*M_PI)/180.0 as CGFloat
let rotatedViewBox = UIView(frame: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
let t = CGAffineTransform(rotationAngle: radians)
rotatedViewBox.transform = t
let rotatedSize = rotatedViewBox.frame.size
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(rotatedSize, false, scale)
let bitmap = UIGraphicsGetCurrentContext()
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
bitmap!.rotate(by: radians);
bitmap!.scaleBy(x: 1.0, y: -1.0);
CGContextDrawImage(bitmap, CGRectMake(-self.size.width/2, -self.size.height/2 , self.size.width, self.size.height), self.CGImage);
let newImage = UIGraphicsGetImageFromCurrentImageContext()
return newImage
}
}
用法image.rotate(degree)
。
谢谢Jason Crocker解决了我的问题。只有一个小小的修正,在这两个位置互换的高度和宽度,并没有出现失真,即
UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height));
[[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:clockwise ? UIImageOrientationRight : UIImageOrientationLeft] drawInRect:CGRectMake(0,0,size.width,size.height)];
我的问题不能由CGContextRotateCTM来解决,我不知道为什么。我的问题是,我正在将我的图像传输到服务器,并一直显示90度。通过将图像复制到您在Mac上运行的MS Office程序,您可以轻松测试图像是否在非苹果世界中运行。
这就是我所做的,当我想改变图像的方向(顺时针旋转90度)。
//Checking for the orientation ie, image taken from camera is in portrait or not.
if(yourImage.imageOrientation==3)
{
//Image is in portrait mode.
yourImage=[self imageToRotate:yourImage RotatedByDegrees:90.0];
}
- (UIImage *)image:(UIImage *)imageToRotate RotatedByDegrees:(CGFloat)degrees
{
CGFloat radians = degrees * (M_PI/180.0);
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, image.size.height, image.size.width)];
CGAffineTransform t = CGAffineTransformMakeRotation(radians);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, [[UIScreen mainScreen] scale]);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.height/2, rotatedSize.width/2);
CGContextRotateCTM(bitmap, radians);
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-image.size.width/2, -image.size.height/2 , image.size.height, image.size.width), image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
旋转后的图像可以是大小> = 15MB(从我的经验)的。所以你应该压缩它并使用它。否则,您可能遇到导致内存压力的崩溃。下面给出了用于压缩的代码。
NSData *imageData = UIImageJPEGRepresentation(yourImage, 1);
//1 - it represents the quality of the image.
NSLog(@"Size of Image(bytes):%d",[imageData length]);
//Here I used a loop because my requirement was, the image size should be <= 4MB.
//So put an iteration for more than 1 time upto when the image size is gets <= 4MB.
for(int loop=0;loop<100;loop++)
{
if([imageData length]>=4194304) //4194304 = 4MB in bytes.
{
imageData=UIImageJPEGRepresentation(yourImage, 0.3);
yourImage=[[UIImage alloc]initWithData:imageData];
}
else
{
NSLog(@"%d time(s) compressed.",loop);
break;
}
}
现在你yourImage
可用于任何地方.. 快乐编码...
- 1. 如何旋转UIImage或UIImageView?
- 2. Xamarin UIImage旋转
- 3. 旋转UIImage?
- 4. UIImage旋转轴
- 5. UIImage旋转
- 6. 旋转UIImage问题
- 7. 帮助旋转UIImage
- 8. UIImage像素旋转
- 9. 如何在图像中心旋转UIImage
- 10. 如何旋转90度的UIImage?
- 11. 旋转UIImage的给予空白的UIImage
- 12. 旋转UIImage自定义度
- 13. UIImage在裁剪后旋转
- 14. UIImage旋转自定义度
- 15. UIImage复制结束旋转
- 16. UIImageView旋转我的UIImage
- 17. 旋转和裁剪UIImage
- 18. 在drawRect中旋转UIImage
- 19. 在iPhone中旋转UIImage
- 20. 旋转UIImage的问题
- 21. 旋转UIImage问题 - IOS/Swift
- 22. 转换UIScrollViewPoint到的UIImage + ZOOM +旋转
- 23. NSUndoManager使用旋转手势撤消UIImage旋转
- 24. 旋转的UIImage并没有显示
- 25. 旋转UIImage没有内存峰值
- 26. UIImage旋转时丢失质量
- 27. 旋转UIImage没有质量损失
- 28. 从旋转的UIImageView创建UIImage
- 29. 如何在旋转UIImage后缩放并获得镜像?
- 30. 如何检测当前方向以旋转UIImage?
我做喜欢说,但是我有一个问题...我从旋转图像左上角,我怎么能设置它从UIImage的中心旋转图像? – 2013-02-13 16:41:39
你可以玩'iv.layer.anchorPoint',但它的默认值是一个图层的中心,所以我不确定它为什么会从左上角旋转图像。 – kovpas 2013-02-13 16:47:17
不,但我使用了您提供给我的链接中的代码。所以这是CGContextRotateCTM – 2013-02-13 16:57:11