简体   繁体   English

CATiledLayer或CALayer绘图UIImage无法正常工作

[英]CATiledLayer or CALayer drawing UIImage not working

Good day all; 大家好!

I am trying to manually (not using sublayers) draw images within a CATiledLayer but it is not behaving as it should with the defined solution. 我正在尝试手动(不使用子层)在CATiledLayer中绘制图像,但是在定义的解决方案中它表现不佳。 I undertand that when you call 'CGContextDrawImage' you must scale and translate so as to flip it but I cannot for the life of me, get it to work. 我强调,当您调用“ CGContextDrawImage”时,必须进行缩放和平移才能翻转它,但我无法终生使用它。

I have a method called 我有一个方法叫做

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

which is called several times from with in 从中被多次调用

 - (void)drawInContext:(CGContextRef)context

to render all the images that go into the CATileLayer. 渲染进入CATileLayer的所有图像。

The following renders no images: 以下内容不显示任何图像:

- (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); 

The following produces an image but rendered incorrectly: 以下生成图像但渲染不正确:

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

Like wise with this: 像这样明智:

- (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);

I have also tried: 我也尝试过:

UIGraphicsBeginImageContext
UIGraphicsEndImageContext

and

UIGraphicsPushContext
UIGraphicsPopContext

All do not work. 都行不通。 I am missing something fundamental here and I suspect my approach to saving the contexts is not working. 我在这里缺少一些基本的东西,我怀疑我保存上下文的方法不起作用。

OK I think I know what is happening here; 好吧,我想我知道这里发生了什么。

It appears that the image is actually rendering but rendering off screen. 看起来图像实际上是在渲染,但在屏幕外渲染。 The flipping is not happening relative to the origin of the layer but rather the origin of the super layer. 翻转不是相对于图层的起点发生的,而是相对于超级图层的起点发生的。

The issue I am seeing is that I have a large layer and a large grid of sublayers. 我看到的问题是我有一个较大的图层和一个较大的子图层网格。 I assumed that a flip within the sublayer, would be relative to it's own co-orindates but that is not the case. 我以为子层内的翻转是相对于其自身的协同关系的,但事实并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM