首页 文章

在ios中将图像旋转90°时出错

提问于
浏览
0

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

- (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 before rotate

当我点击旋转时,它旋转不好 -
image after rotate

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

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

2 回答

  • 0

    当您的图像是纵向时,请勿应用旋转(或应用π旋转) . 您无需像调用它一样更改 rotateImage 实现 .

  • 0

    我认为UIImageOrientationLeft和UIImageOrientationRight存在问题,其他所有问题,即UIImageOrientationUp和UIImageOrientationDown都与它们看起来一样 .

    我认为代码中没有问题,不要使用UIImageOrientationLeft和UIImageOrientationRight . 问题将得到解决 .

相关问题