【问题标题】:iOS: How to rotate CGImage and draw it rotated onto UIImage? [duplicate]iOS:如何旋转 CGImage 并将其绘制到 UIImage 上? [复制]
【发布时间】:2012-10-19 21:28:59
【问题描述】:

我已经为此苦苦挣扎了好几个小时。仿射变换似乎从来没有像我期望的那样。

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 会以一种糟糕的方式拉伸。如果我先平移然后旋转,结果相同。

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

【问题讨论】:

    标签: ios uiimage cgaffinetransform cgimage


    【解决方案1】:

    此代码目前在 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];
    
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多