如何设置一个用户Quartz2D坐标系统,缩放,避免模糊绘图? [英] How to set up a user Quartz2D coordinate system with scaling that avoids fuzzy drawing?
问题描述
此主题已被抓取一两次,但我仍然困惑。 < / p>
由于Quartz允许使用仿射变换的任意坐标系,因此我希望能够使用现实生活的坐标绘制诸如平面布置图之类的东西,例如
基本上,为了举例,我想缩放视图,这样当我绘制一个10×10的矩形),我得到一个60x60像素的矩形。
它工作,除了我得到的矩形是模糊的。这里的另一个问题有一个解释为什么的答案。然而,我不知道我理解为什么,而且,我不知道如何解决它。这是我的代码:
我在我的 awakeFromNib
自定义视图方法中设置了坐标系:
- (void)awakeFromNib {
CGAffineTransform scale = CGAffineTransformMakeScale(6.0,6.0);
self.transform = scale;
}
这是我的绘制例程:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext
CGRect r = CGRectMake(10。,10.,10.,10.);
CGFloat lineWidth = 1.0;
CGContextStrokeRectWithWidth(context,r,lineWidth);
}
我得到的方块是很好,但完全模糊。使用 lineWidth
不起作用:当 lineWidth
设置得更小时,它变得更轻, p>
那么有没有办法设置一个视图来缩放坐标系,这样我可以使用我的域坐标?
请注意,这个问题不会发生在翻译或轮换。
感谢
问题是视图transform属性在绘制到位缓冲区后应用于它。缩放变换必须在绘制之前应用,即。在 drawRect
方法中。所以擦除 awakeFromNib
我给,这里是一个正确的drawRect:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform scale = CGAffineTransformMakeScale(6.0,6.0);
CGContextConcatCTM(context,scale);
CGRect r = CGRectMake(10。,10.,10.,10.);
CGFloat lineWidth = 0.1;
CGContextStrokeRectWithWidth(context,r,lineWidth);
}
This topic has been scratched once or twice, but I am still puzzled. And Google was not friendly either.
Since Quartz allows for arbitrary coordinate systems using affine transforms, I want to be able to draw things such as floorplans using real-life coordinate, e.g. feet.
So basically, for the sake of an example, I want to scale the view so that when I draw a 10x10 rectangle (think a 10-inch box for example), I get a 60x60 pixels rectangle.
It works, except the rectangle I get is quite fuzzy. Another question here got an answer that explains why. However, I'm not sure I understood that reason why, and moreover, I don't know how to fix it. Here is my code:
I set my coordinate system in my awakeFromNib
custom view method:
- (void) awakeFromNib {
CGAffineTransform scale = CGAffineTransformMakeScale(6.0, 6.0);
self.transform = scale;
}
And here is my draw routine:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect r = CGRectMake(10., 10., 10., 10.);
CGFloat lineWidth = 1.0;
CGContextStrokeRectWithWidth(context, r, lineWidth);
}
The square I get is scaled just fine, but totally fuzzy. Playing with lineWidth
doesn't help: when lineWidth
is set smaller, it gets lighter, but not crisper.
So is there a way to set up a view to have a scaled coordinate system, so that I can use my domain coordinates? Or should I go back and implementing scaling in my drawing routines?
Note that this issue doesn't occur for translation or rotation.
Thanks
Well, as often, explaining the issue lead me to a solution.
The problem is that the view transform property is applied to it after it has been drawn into a bit buffer. The scaling transform has to be applied before drawing, ie. in the drawRect
method. So scratch the awakeFromNib
I gave, and here is a correct drawRect:
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform scale = CGAffineTransformMakeScale(6.0, 6.0);
CGContextConcatCTM(context, scale);
CGRect r = CGRectMake(10., 10., 10., 10.);
CGFloat lineWidth = 0.1;
CGContextStrokeRectWithWidth(context, r, lineWidth);
}
这篇关于如何设置一个用户Quartz2D坐标系统,缩放,避免模糊绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!