iOS:如何旋转CGImage并将其旋转到UIImage上?
问题描述:
我一直在挣扎几个小时。仿射变换从未像我期望的那样做。iOS:如何旋转CGImage并将其旋转到UIImage上?
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);
我有一个名为图像的UIImage,我有一个可通过ctx访问的CGImage。我想从一个角度旋转UIImage的中心,并将其绘制在CGImage的顶部。
如果我旋转,然后翻译,UIImage得到了一个坏的方式streteched。如果我翻译,然后旋转,结果相同。
任何线索如何做到这一点?谢谢。
答
此代码目前正在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];
}
}