无质量损失地旋转 UIImage
Rotate UIImage without quality loss
我在不损失质量的情况下旋转 UIImage 时遇到问题。现在我正在使用 BFKit imageRotatedByDegrees: 提供的现成方法,但我的图像变得模糊(模糊)。
示例代码:
UIImage *image = [[UIImage imageNamed:@"car"] imageRotatedByDegrees:(((float)arc4random() / ARC4RANDOM_MAX) * (360))];
imageRotatedByDegrees 的代码::
- (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?
我的目标是旋转我的 MKAnnotationView 中的图像。所以我通过旋转不是图像而是整个 MKAnnotationView 解决了这个问题。
一个对我有帮助的答案是 this。
我在 MKMapViewDelegate 方法中的代码 mapView:viewForAnnotation::
annotationView.transform = CGAffineTransformMakeRotation((((float)arc4random() / ARC4RANDOM_MAX) * (360)));
我是 BFKit 的作者,当我读到你的问题时,我调查了那个问题。
在最新版本 (1.6.4) 中,我已修复该问题!
现在图像将不再被漫射(模糊)。
我在不损失质量的情况下旋转 UIImage 时遇到问题。现在我正在使用 BFKit imageRotatedByDegrees: 提供的现成方法,但我的图像变得模糊(模糊)。
示例代码:
UIImage *image = [[UIImage imageNamed:@"car"] imageRotatedByDegrees:(((float)arc4random() / ARC4RANDOM_MAX) * (360))];
imageRotatedByDegrees 的代码::
- (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?
我的目标是旋转我的 MKAnnotationView 中的图像。所以我通过旋转不是图像而是整个 MKAnnotationView 解决了这个问题。
一个对我有帮助的答案是 this。
我在 MKMapViewDelegate 方法中的代码 mapView:viewForAnnotation::
annotationView.transform = CGAffineTransformMakeRotation((((float)arc4random() / ARC4RANDOM_MAX) * (360)));
我是 BFKit 的作者,当我读到你的问题时,我调查了那个问题。 在最新版本 (1.6.4) 中,我已修复该问题!
现在图像将不再被漫射(模糊)。