【问题标题】:Rotating CIImage around a point围绕一个点旋转 CIImage
【发布时间】: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


【解决方案1】:

要旋转 UIImage 图像,比如说 90 度,您可以轻松做到:

imageView.transform = CGAffineTransformMakeRotation(M_PI/2);

您可以多次旋转它:

UIView animateKeyframesWithDuration method 

并将其锚定到您可以使用的某个点:

[image.layer setAnchorPoint:CGPointMake(....)];

【讨论】:

  • 我不想更改/操作 UIImageView,我正在尝试更改实际的底层 UIImage。
猜你喜欢
  • 2023-04-03
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 2013-05-20
  • 1970-01-01
相关资源
最近更新 更多