【问题标题】:error rotating image by 90 in ios在 ios 中将图像旋转 90 时出错
【发布时间】:2014-08-30 04:40:40
【问题描述】:

我正在使用以下代码来旋转图像 -

- (UIImage *)rotateImage:(UIImage*)image byDegree:(CGFloat)degrees
{

UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width,    image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(M_PI/2);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;

UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();

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

    CGContextRotateCTM(bitmap, M_PI/2);
    CGContextScaleCTM(bitmap, 1.0, -1.0);

CGContextDrawImage(bitmap, CGRectMake(-image.size.width/2, -image.size.height/2, image.size.width, image.size.height), [image CGImage]);

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;

}

我的原始图像是 -

当我点击旋转时,它不能很好地旋转 -

这种行为仅在图像处于纵向模式时发生,即 (image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationRight) 。横向模式图像旋转良好。

如何编辑代码来处理这两种情况?

【问题讨论】:

    标签: ios objective-c image rotation uiimage


    【解决方案1】:

    我认为 UIImageOrientationLeft 和 UIImageOrientationRight 存在问题,所有其他,即 UIImageOrientationUp 和 UIImageOrientationDown 的外观都相同。

    我认为代码没有问题,不要使用 UIImageOrientationLeft 和 UIImageOrientationRight。问题会解决的。

    【讨论】:

    • 我没有在我的代码中使用 UIIamgeOrientations 进行旋转。当图像是横向视图(宽度>高度或手机水平点击照片)时,代码运行良好。但是,当图像是纵向(高度>宽度或用手机垂直单击照片)时,在第一次调用旋转时,它会扭曲图像(如第二张图像所示),然后在随后的调用中,它会旋转 90 度(在相同的扭曲视图)。
    • 上面的代码似乎是对的,似乎没有任何错误。正如您所说的失真仅在第一次调用时出现,这也可能意味着代码的其他部分(此处未显示)可能导致失真,随后发生旋转。我检查了代码,它似乎在纵向和横向上都可以正常工作。
    【解决方案2】:

    当您的图像为纵向时,请勿应用旋转(或应用 π 旋转)。您无需更改 rotateImage 实现,只需按照您的调用方式即可。

    【讨论】:

      猜你喜欢
      • 2012-05-05
      • 1970-01-01
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      相关资源
      最近更新 更多