• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

以角度旋转UIImageView

IOS 来源:Alessandro 4次浏览

我想通过按下按钮来旋转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]; 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)