iOS UIImage 图像倒置

7
如果我画我的图像,我可以使用CGAffintrasform来解决问题。
CGAffineTransform myTr = CGAffineTransformMake(1, 0, 0, -1, 0, backImage.size.height);
CGContextConcatCTM(context, myTr);
[backImage drawInRect:CGRectMake(cbx, -cby, backImage.size.width, backImage.size.height)];
myTr = CGAffineTransformMake(1, 0, 0, -1, 0, backImage.size.height);
CGContextConcatCTM(context, myTr);

当我想要写入文件时,我使用以下代码:
 NSData *imageData = UIImageJPEGRepresentation(backImage, 0);  

然后图片倒置,如何操作?

你从哪里获取backImage?你不能只是使用一次变换来翻转上下文吗? - Fabian Kreiser
我画它,就像CGContextBeginPath... CGContextMove...(0,0) CGContextAddLines... CGContextFillPath... - zt9788
2个回答

2

当您想获取UIImage以保存时,请使用:

UIGraphicsBeginImageContextWithOptions(size, isOpaque, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, (CGRect){ {0,0}, origSize }, [origImage CGImage]);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;

接下来执行:

 NSData *imageData = UIImageJPEGRepresentation(backImage, 0);

在WWDC上,苹果一再告诉我们在渲染图像时要使用上述方法。 - David H

2

首先创建一个单位矩阵。

1, 0, 0
0, 1, 0
0, 0, 1

CGAffineTransform matrix = CGAffineTransformMake(1, 0, 0, 1, 0, 0);

移动绘图位置...

matrix = CGAffineTransformTranslate(matrix, x, y);

水平翻转矩阵。

matrix = CGAffineTransformScale(matrix, -1, 1);

垂直翻转矩阵。

matrix = CGAffineTransformScale(matrix, 1, -1);

旋转矩阵

matrix = CGAffineTransformRotate(matrix, angle);

将UIImage适配到UIView的比例。

matrix = CGAffineTransformScale(matrix, imageWidth/viewWidth, imageheight/viewHeight);

将矩阵投射到上下文中。
CGContextConcatCTM(context, matrix);

画图像。
[backImage drawAtPoint:CGPointMake(0, 0)];

:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接