CATiledLayer或CALayer绘制UIImage不起作

CATiledLayer或CALayer绘制UIImage不起作

本文介绍了CATiledLayer或CALayer绘制UIImage不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天;

我正在尝试手动(不使用子图层)在CATiledLayer中绘制图像,但它的行为与定义的解决方案不同。我承诺,当你调用'CGContextDrawImage'时,你必须进行缩放和翻译才能翻转它,但我不能为我的生活,让它工作。

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. 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.

我有一个名为

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

多次从中调用

 - (void)drawInContext:(CGContextRef)context

渲染所有图像进入CATileLayer。

to render all the images that go into the 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);

与此类似:

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

一切都行不通。我在这里缺少一些基本的东西,我怀疑我保存上下文的方法不起作用。

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.

这篇关于CATiledLayer或CALayer绘制UIImage不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:50