CGContext 总结二

CGContext 总结二

CGContext中的transform

1.1 用法

CGContextScaleCTM(ref, 1.0, -1.0)表示缩放,为负数时,整个翻转;
CGContextRotateCTM(ref, 180 * M_PI/180)表示旋转,rotate的方向,为正时,旋转方向为从x轴到y轴,负则相反;
CGContextTranslateCTM(ref, 0, -400)表示平移;
也可结合CGAffineTransform:

transform = CGAffineTransformMakeTranslation(300, 0.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
CGContextConcatCTM(ref, transform);

效果与前者一致。需注意的是,以上transform都是对context坐标系的操作,即坐标原点会跟着变动。

1.2 示例

//right 图片向右旋转
CGContextScaleCTM(ref, -1, 1);
CGContextTranslateCTM(ref, -300, 0);//300代表宽
transform = CGAffineTransformMakeTranslation(300, 0.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
CGContextConcatCTM(ref, transform);

//left 图片向左旋转
CGContextScaleCTM(ref, -1, 1);
CGContextTranslateCTM(ref, -300, 0);
transform = CGAffineTransformMakeTranslation(0.0, 400);//400代表高
transform = CGAffineTransformRotate(transform, -90 * M_PI/180);
//transform = CGAffineTransformMakeTranslation(0.0, 400);
CGContextConcatCTM(ref, transform);

以上都是基于context坐标系原点在左上角:UIGraphicsBeginImageContext(CGSizeMake(300, 400)),且采用CGContextDrawImage(ref, CGRectMake(0, 0, 300, 400), newImage.CGImage);;还有另外一种方式实现图片旋转,即UIImage的drawInRect:方法,此方法则不用对context进行操作:

UIGraphicsBeginImageContext(CGSizeMake(300, 400));
UIImage *originImage = [UIImage imageNamed:@"D.jpeg"];
UIImage *newImage = [UIImage imageWithCGImage:originImag
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值