【发布时间】:2015-06-09 14:25:00
【问题描述】:
我想从文件(文件系统上的 jpeg)中旋转一个 UIImage 计算出的弧度,但我想围绕图像中的一个点旋转它,并保持图像的原始大小(在图像数据不再存在的图像中有透明间隙,以及裁剪已移出原始帧的图像数据)。然后我想存储并显示生成的 UIImage。我还没有找到任何资源来完成这项任务,任何帮助将不胜感激!
到目前为止我发现的最接近的东西(稍作修改)如下:
-(UIImage*)rotateImage:(UIImage*)image aroundPoint:(CGPoint)point radians:(float)radians newSize:(CGRect)newSize { CGRect imageRect = { point, image.size };
UIGraphicsBeginImageContext(image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, imageRect.origin.x, imageRect.origin.y);
CGContextRotateCTM(context, radians);
CGContextTranslateCTM(context, -imageRect.origin.x, -imageRect.origin.y);
CGContextDrawImage(context, (CGRect){ CGPointZero, imageRect.size }, [image CGImage]);
UIImage *returnImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return returnImg;
}
不幸的是,这会错误地旋转图像(在我的测试中,某处比预期的多 180 度)。
【问题讨论】:
-
添加了我当前的代码,这导致图像比预期旋转了 180 度。
-
另外,如果我将旋转设置为:CGContextRotateCTM(context, radians + M_PI);它已正确旋转,但向右移动。
-
你是偶然解决的吗?
标签: ios objective-c uiimage image-rotation