grabbing layer contents in a class

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-04-25T04:01:36Z Indexed on 2010/04/25 4:03 UTC
Read the original article Hit count: 277

I have a custom UIImageView class that creates thumbnails (UIImageView) and each thumbnail has a label.

The label is created by another class. The label class creates a UIImageView and a UITextView on top of it. This is the init object class' init method:

- (id)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame] == nil) {
        return nil;
    }

 CGRect myFrame = CGRectMake(0, 0, 100, 100);
 myLabel = [[myLabelClass alloc] initWithFrame: myFrame]; //myLabelClass is a UIImageView based class
 [myLabel setCenter:CGPointMake(50, 50)];
 [self addSubview: myLabel];

  return self;
}

So, I have this

 MAIN VIEW CONTROLLER   |   
                        |___ UIImageView WITH LABEL
                                                |
                                                |____ label background (UIView)
                                                |____ UITEXTVIEW (text)

Now I want to write the contents of all these 3 components to a quartz context.

I need to write using drawInRect, because I need to write the full object to a precise location.

I expected object.layer.contents to be the image equivalent to these 3 "layers" flattened, in other words, the object's image, the label background and the label text, as it would if I created these 3 objects in Photoshop and flatten the composition.

I also expect object.myLabel.layer.contents to contains the rendered contents of the UITextView over the label background.

The problem is that when I use

UIImage *myImage = [UIImage imageWithCGImage:objecto.myLabel.layer.contents];
[myImage drawInRect:CGRectMake(0, 0, 100, 100)];

I get nothing.

How can I obtain a "flattened" image resultant to the full object (including its subviews)?

thanks

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk