【问题标题】:CATiledLayer or CALayer drawing UIImage not workingCATiledLayer 或 CALayer 绘图 UIImage 不起作用
【发布时间】:2010-07-23 15:45:27
【问题描述】:

大家好;

我正在尝试手动(不使用子图层)在 CATiledLayer 中绘制图像,但它的行为与定义的解决方案不同。我知道,当您调用“CGContextDrawImage”时,您必须缩放和平移以翻转它,但我无法终生使用它。

我有一个方法叫

 - (void)drawInContext:(CGContextRef)context Image:(UIImage *)image

中被多次调用
 - (void)drawInContext:(CGContextRef)context

渲染所有进入 CATileLayer 的图像。

以下不渲染图像:

- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image  {
    CGRect rect = CGRectMake(x, y, image.size.width, image.size.height);
    CGContextSaveGState(context);

    CGContextTranslateCTM(context, 0, image.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, rect, image.CGImage);

    CGContextRestoreGState(context); 

以下生成图像但渲染不正确:

- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image  {
     CGRect rect = CGRectMake(x, y, image.size.width, image.size.height);
     CGContextDrawImage(context, rect, image.CGImage);

明智地这样做:

- (void)drawInContext:(CGContextRef)context Image:(UIImage *)image {
    CGRect rect = CGRectMake(x, y, image.size.width, image.size.height);

    CGContextTranslateCTM(context, 0, image.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, rect, image.CGImage);

我也试过了:

UIGraphicsBeginImageContext
UIGraphicsEndImageContext

UIGraphicsPushContext
UIGraphicsPopContext

所有都不起作用。我在这里遗漏了一些基本的东西,我怀疑我保存上下文的方法不起作用。

【问题讨论】:

    标签: iphone quartz-graphics ios4


    【解决方案1】:

    好的,我想我知道这里发生了什么;

    看起来图像实际上正在渲染,但在屏幕外渲染。翻转不是相对于层的原点发生的,而是相对于超级层的原点发生的。

    我看到的问题是我有一个大图层和一个大网格的子图层。我假设子层内的翻转将与它自己的坐标相关,但事实并非如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多