Duck
Duck

Reputation: 36003

iphone . loading a PNG into a CGLayer not working

I have a CGLayer that was created like this:

CGSize size = CGSizeMake(500, 500);

UIGraphicsBeginImageContext(tamanho);
ctx = UIGraphicsGetCurrentContext();
[self loadImageToCTX]; // this method loads an image into CTX

lineLayer = CGLayerCreateWithContext (ctx, size, NULL);

Now I have a PNG that has alpha and some content. I need to load this PNG into lineLayer, so I do...

// lineLayer is empty, lets load a PNG into it
CGRect superRect = CGRectMake(0,0, 500, 500);

CGContextRef lineContext = CGLayerGetContext (lineLayer);
CGContextSaveGState(lineContext);

// inverting Y, so image will not be loaded flipped 
CGContextTranslateCTM(lineContext, 0, -500);
CGContextScaleCTM(lineContext, 1.0, -1.0);

//  CGContextClearRect(lineContext, superRect);

UIImage *loaded = [self recuperarImage:@"LineLayer.png"];
CGContextDrawImage(lineContext, superRect, loaded.CGImage);
CGContextRestoreGState(lineContext);

if I render, at this point, the contents of ctx + lineLayer, the final image contains just ctx...

// if I render the contents to a view using the lines below, I see just CTX, lineLayer contents are not there
// remember CTX has an image and lineLayer has a transparent loaded PNG
// but when I render this, the final image contains just CTX's contents...
// this is how it is rendered.

CGContextDrawLayerInRect(ctx, superRect, lineLayer);
myView.image = UIGraphicsGetImageFromCurrentImageContext();

am I missing something? thanks in advance.

Upvotes: 0

Views: 369

Answers (2)

Duck
Duck

Reputation: 36003

I figured the problem. the lines

CGContextTranslateCTM(lineContext, 0, -500);
CGContextScaleCTM(lineContext, 1.0, -1.0);

are in the wrong order... you have to scale before translating, or the layer will end outside the context region...

thank you guys!

Upvotes: 0

tc.
tc.

Reputation: 33602

I'm not entirely sure what this line is doing:

lineLayer = CGLayerCreateWithContext (ctx, size, NULL);

Why are you reusing ctx? My reading is that it will mean lineContext == ctx, so the call to CGContextDrawLayerInRect() draws the context's contents into itself, which can't be very good (and might not be handled properly).

It's also worth checking that "loaded" is non-nil.

Also, that's a lot of work to draw an image. Just do something like

UIGraphicsBeginImageContext(tamanho);
ctx = UIGraphicsGetCurrentContext();
[self loadImageToCTX];
[[UIImage imageNamed:@"LineLayer.png"] drawInRect:(CGRect){{0,0},{500,0}}];
myView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Upvotes: 1

Related Questions