CATiledLayer or CALayer drawing UIImage not working

ε祈祈猫儿з 提交于 2019-12-13 02:53:05

问题


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

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.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/3319835/catiledlayer-or-calayer-drawing-uiimage-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!