【发布时间】:2012-03-17 23:49:01
【问题描述】:
我真的在 Quartz2D 上苦苦挣扎了 10 多天,请帮助我理解一些概念,我将非常感激,请查看此代码和屏幕截图 url。
此代码绘制带边框的图像并向其写入文本,图像成为带边框和文本的全新图像。
//part 1
CGSize cgs = CGSizeMake(250.0, 400.0);
UIGraphicsBeginImageContext(cgs);
CGRect rectangle = CGRectMake(0,0,cgs.width,cgs.height);
CGRect imageRect = CGRectInset(rectangle, 5.4, 5.4);
imageRect.size.height -= 100;
UIImage *myImage = [UIImage imageNamed:@"BMW.jpg"];
[myImage drawInRect:imageRect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextStrokeRect(context, rectangle);
//
//part 2
1. CGRect contextRect = rectangle;
2. CGContextTranslateCTM(context, 0, contextRect.size.height);
3. CGContextScaleCTM(context, 1, -1);
4. float w, h;
5. w = contextRect.size.width;
6. h = contextRect.size.height;
7. CGContextSelectFont (context, "Helvetica-Bold", 25,
kCGEncodingMacRoman);
8. CGContextSetCharacterSpacing (context, 5);
9. CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1.0);
10. CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
11. CGContextShowTextAtPoint(context, 45, 50, "Quartz 2D", 9);
//
//part 3
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[testImg drawAtPoint:CGPointMake(35, 10)];
//
http://i40.tinypic.com/140aptv.png
代码的第 1 部分和第 3 部分对我来说非常清楚
问题在于第 2 部分
第 2 行和第 3 行坐标已转换,因此文本不显示 颠倒了,但是 uiimage 已经在内部处理了这个问题,为什么它 不是倒转过来了吗?为什么它仍然显示在 转换后的正确位置适用于使用相同的文本 语境?我问这个是因为当 uiimage 坐标已经 修改然后这个坐标变换不会再次生成uiimage 倒过来?
在第 9 行和第 10 行调用了 fillcolor 和 strokecolor 方法,并且 fillcolor 更改文本颜色,但 strokecolor 没有做任何事情 发短信为什么?以及为什么没有 CGContextSaveGState 它修改了 文本颜色不是边框颜色?
关于我上面提到的这两点,常见的混淆是 为什么它工作得很好为什么不需要这段代码 CGContextSaveGState 和 CGContextRestoreGState。怎么可能 该上下文已被修改,并且不会影响先前的绘图 在这种情况下,像蓝色边框这样的项目和坐标转换 文本。
如果我在任何方面缺乏让您理解我的观点的地方,请纠正我。
提前致谢, 问候。
【问题讨论】:
标签: ios core-graphics