【问题标题】:IOS UIImage Image upside downIOS UIImage 图片倒置
【发布时间】:2012-07-29 04:51:09
【问题描述】:

如果我绘制我的图像,我使用 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?你不能只使用一次转换来翻转上下文吗?
  • 我画了它,就像 CGContextBeginPath.... CGContextMove..(0,0) CGContextAddLines....CGContextFillPath.....

标签: ios uiimage jpeg draw


【解决方案1】:

当你想保存一个 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);

【讨论】:

  • Apple 在 WWDC 上一遍又一遍地告诉我们在渲染图像时使用上述内容。
【解决方案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)];

:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    相关资源
    最近更新 更多