旋转图像上下文 (CGContextRotateCTM) 导致其变为空白。为什么?
#define radians(degrees) (degrees * M_PI/180)
UIImage *rotate(UIImage *image) {
CGSize size = image.size;;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
// If this is commented out, image is returned as it is.
CGContextRotateCTM (context, radians(90));
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
会不会还有什么问题?没主意了。
#define radians(degrees) (degrees * M_PI/180)
UIImage *rotate(UIImage *image) {
CGSize size = image.size;;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
// If this is commented out, image is returned as it is.
CGContextRotateCTM (context, radians(90));
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Could something else be wrong? Out of ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是你的上下文正在围绕(0,0)(即左上角)旋转。如果绕左上角旋转 90 度,则所有绘图都将超出上下文范围。您需要一个两步变换将上下文的原点移动到中间,然后旋转。此外,您还需要以移动/旋转的原点为中心绘制图像,如下所示:
HTH
The problem is your context is being rotated around (0,0) which is the top left corner. If you rotate 90 degrees around the top left corner, all your drawing will occur out of the bounds of the context. You need a 2-step transform to move the origin of the context to it's middle, and THEN rotate. Also you need to draw your image centered around the moved/rotated origin, like this:
HTH
我尝试遵循代码,它有效,从 http://www.catamount.com/blog/1015/uiimage-extensions-for-cutting-scaling-and-rotating-uiimages/
I try followed code, it works, get from http://www.catamount.com/blog/1015/uiimage-extensions-for-cutting-scaling-and-rotating-uiimages/
如果您不想更改所有原点和大小,请移动中心,旋转并再次将中心更改为角,如下所示:
CGContextTranslateCTM( context, 0.5f * size.width, 0.5f * size.height ) ;
CGContextRotateCTM(上下文,弧度(90));
CGContextTranslateCTM( context, -0.5f * size.width, -0.5f * size.height ) ;
然后像这样正常绘制:
[image drawInRect:CGRectMake(0, 0, size.width, size.高度)];
If you don't want to change all the origins and sizes, move the center, rotate and change the center to corner again like this:
CGContextTranslateCTM( context, 0.5f * size.width, 0.5f * size.height ) ;
CGContextRotateCTM( context, radians( 90 ) ) ;
CGContextTranslateCTM( context, -0.5f * size.width, -0.5f * size.height ) ;
Then draw normally like this:
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
扩展 Nielsbot 的答案,我创建了以下代码片段。请注意,最好将其设为函数而不是代码片段,以防止“复制/粘贴编程”。我还没有做到这一点,所以请随意适应它。
代码片段:
另外,我欢迎有更好的方法将 CGContext 重置为其之前的状态。
Expanding on Nielsbot's answer, I created the following snippet. Note that this should preferably be made a function rather than a code snippet, to prevent 'copy/paste programming'. I didn't get to that yet, so feel free to adapt it.
Code snippet:
Also, I welcome a better way to reset the CGContext to its previous state.
我喜欢 @lbsweek 的解决方案,但我有点担心只是为了转换而分配 UIView,而是尝试了下面的方法
I liked the solution by @lbsweek but I was bit worried about alloc ing UIView just for transform, instead I tried the below