【问题标题】:Rotate UIImage without quality loss旋转 UIImage 而不损失质量
【发布时间】:2015-03-02 06:23:35
【问题描述】:

在不损失质量的情况下旋转 UIImage 时遇到问题。现在我正在使用 BFKit imageRotatedByDegrees: 提供的 ready 方法,但是我的图像变得分散(模糊)。

示例代码:

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 而不损失质量吗?

【问题讨论】:

    标签: ios uiimage


    【解决方案1】:

    我是 BFKit 的作者,当我读到您的问题时,我就该问题进行了调查。 在最新版本 (1.6.4) 中,我已经解决了这个问题!

    现在图像将不再被扩散(模糊)。

    【讨论】:

      【解决方案2】:

      我的目标是旋转 MKAnnotationView 中的图像。所以我通过旋转不是图像,而是整个 MKAnnotationView 来解决问题。

      对我有帮助的答案是this

      我在 MKMapViewDelegate 方法中的代码 ma​​pView:viewForAnnotation::

      annotationView.transform = CGAffineTransformMakeRotation((((float)arc4random() / ARC4RANDOM_MAX) * (360)));
      

      【讨论】:

        猜你喜欢
        • 2012-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-03
        • 2014-10-23
        • 2012-07-13
        • 2014-03-22
        • 1970-01-01
        相关资源
        最近更新 更多