iOS:如何旋转 CGImage 并将其绘制到 UIImage 上?

标签 ios uiimage cgaffinetransform cgimage

<分区>

我已经为此苦苦挣扎了几个小时。仿射变换似乎从来没有按照我的预期去做。

CGContextSaveGState (ctx);
float w = self.frame.size.width;
float h = self.frame.size.height;
CGContextRotateCTM (ctx, angle);
CGContextTranslateCTM (ctx, w/2, -h/2);
CGRect r = CGRectMake (0,0, w, h);
CGContextDrawImage (ctx, r, image.CGImage);
CGContextRestoreGState (ctx);

我有一个名为 image 的 UIImage,我有一个可通过 ctx 访问的 CGImage。我想将 UIImage 从其中心旋转一个角度,并将其绘制在 CGImage 的顶部。

如果我旋转然后平移,UIImage 会以糟糕的方式拉伸(stretch)。如果我平移然后旋转,结果相同。

关于如何做到这一点的任何线索?谢谢。

最佳答案

此代码目前可在 iOS6 中运行。

检查图像宽度是否大于其高度,如果是,则顺时针旋转图像;否则它将按照默认行为加载。

- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = myImage;

if( image.size.width > image.size.height ) {

    UIImage *image = myImage;
    image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationRight];

    [bgImage setImage:image];

} else {

    [bgImage setImage:image];

}

}

关于iOS:如何旋转 CGImage 并将其绘制到 UIImage 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12982713/

相关文章:

ios - UIImage imageNamed内存泄漏

ios - UIImage 如何使用 capInsets 调整气泡图像的大小?

iphone - UIImageView 仅在我调用 initWithImage 时显示

ios - 在 IOS 中沿水平轴翻转前置摄像头录制的视频

ios - 从角落调整旋转的 UIView 的大小

iphone - 为什么我的 UITableView 每个部分只显示两行?

ios - 当我在 Xcode 中设置 "Horizontal centre in container"时,为什么框架会跳到 View 的顶部?

ios - 如何从sqlite中获取数据到数组中

ios - 具有 2 个数据集的多折线图

iphone - 获取 UIView 当前的倾斜角度