【问题标题】:UIImage Rotation custom degreesUIImage Rotation 自定义度数
【发布时间】:2010-10-29 09:32:24
【问题描述】:

我一直在使用此示例中的代码来提供帮助,并且效果很好。 http://www.platinumball.net/blog/2009/03/30/iphone-uiimage-rotation-and-mirroring/

我无法训练如何在 0 到 360 度之间旋转自定义度数......

【问题讨论】:

  • 我会在这里推荐一个 iphone 标签……
  • 一段很棒的代码。不进行任意旋转,但它很有效,并且可以很好地处理镜像的图像方向。

标签: iphone objective-c uiimage image-manipulation


【解决方案1】:

你会想要做与那篇文章中在旋转中所做的几乎相同的事情:

CGSize size = sizeOfImage;
UIGraphicsBeginImageContext(size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextRotateCTM(ctx, angleInRadians);
CGContextDrawImage(ctx, (CGRect){{}, size}, image);

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

除了旋转以补偿旋转中心之外,您可能还需要平移 CTM。如果你不想在旋转时裁剪图像的边缘,你应该通过一些基本的触发来增加尺寸。

【讨论】:

  • ctx 只是图形上下文。我更新了来源以表明这一点。
  • @AndrewPouliot:这真的很棒。但是我如何设置图像的旋转中心?
  • @Shantanu: CGContextTranslateCTM(ctx, -centerOfRotation.x, -centerOfRotation.y),旋转,然后 CGContextTranslateCTM(ctx, centerOfRotation.x, centerOfRotation.y);
  • 为什么要用UIGraphicsGetCurrentContext()存储对当前图形上下文的引用,然后两行后再运行UIGraphicsGetCurrentContext()
  • UIGraphicsGetImageFromCurrentImageContext() 返回 nil,因此图像为 nil。你会如何解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 2013-05-16
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多