以角度旋转UIImageView

我想通过按下按钮来旋转UIImageView。我使用此代码:以角度旋转UIImageView

#import "UIImage-Extensions.h" 

UIImage *rotatedImage = [SplashItGroupPicker.image imageRotatedByDegrees:360/arrayCount];

SplashItGroupPicker.image = rotatedImage;

和 “的UIImage-Extensions.h” 我有:

@interface UIImage (CS_Extensions) 

- (UIImage *)imageRotatedByRadians:(CGFloat)radians;

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;

,并在.M:

- (UIImage *)imageRotatedByRadians:(CGFloat)radians 

{

return [self imageRotatedByDegrees:RadiansToDegrees(radians)];

}

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees

{

// calculate the size of the rotated view's containing box for our drawing space

UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];

CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));

rotatedViewBox.transform = t;

CGSize rotatedSize = rotatedViewBox.frame.size;

// Create the bitmap context

UIGraphicsBeginImageContext(rotatedSize);

CGContextRef bitmap = UIGraphicsGetCurrentContext();

// Move the origin to the middle of the image so we will rotate and scale around the center.

CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

// // Rotate the image context

CGContextRotateCTM(bitmap, DegreesToRadians(degrees));

// Now, draw the rotated/scaled image into the context

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;

}

...旋转工作正常,但当图像旋转时,图像也变小,我不明白为什么。有任何想法吗?

回答:

这是一个猜测,这是唯一发生在视网膜屏幕上吗?每当从图形上下文中保存UIImage时,都需要确保比例是正确的。这是我如何做它:

CGFloat scale = [[UIScreen mainScreen] scale]; 

CGSize size = CGSizeMake(rotatedViewBox.frame.size.width * scale, rotatedViewBox.frame.size.height * scale);

UIGraphicsBeginImageContext(size);

/* Do your image manipulations here */

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

// overwrite the image with one that has the correct scale set

newImage = [UIImage imageWithCGImage:newImage.CGImage scale:scale orientation:newImage.imageOrientation];

回答:

使用Kekoa的片段(感谢一吨),我是能够得到一个工作rotateImage功能,基于苹果在视网膜显示设备的工作原理rotateImage代码:

- (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;

}

回答:

有没有必要UIGraphicsGetImageFromCurrentImageContext。它是获取屏幕所需部分屏幕截图的一种方式。所以结果图像质量受到影响。获得旋转图像的唯一方法就是...

首先由图像旋转视图(如果你想显示旋转视图)

[mainImgV setTransform:CGAffineTransformRotate(mainImgV.transform, M_PI)]; 

它会旋转你的ImageView。 现在你将这些行采取这种旋转图像。(你也可以调用这些线不旋转imgaeview。)

CGImageRef cgRef = mainImgV.image.CGImage; 

newImage = [[UIImage alloc] initWithCGImage:cgRef scale:1.0 orientation:UIImageOrientationDown];

没有必要保存或采取截图或妥协质量差的形象。只是享受编码。

以上是 以角度旋转UIImageView 的全部内容, 来源链接: utcz.com/qa/259569.html

回到顶部