views:

798

answers:

2

i want to get a multiple line string from a UITextView and draw it on a image, the position of text in the image must exactly same as the position in UITextView but i dont want jus add the textView to UIImageView, i need a new image consists of those text, is it possible? any suggestion to do that?

+1  A: 

You can capture the contents of any UIView into a UIImage. First, create an empty UIView and position your UIImageView and UITextView as subviews:

// Assumes you have UIImageView *myImageView and UITextField *myTextField
UIView *parentView = [[UIView alloc] initWithFrame:CGRectZero];
[parentView addSubview:myImageView];
[myImageView setFrame:CGRectMake(0, 0, 100, 100)];
[parentView addSubview:myTextField];
[myTextField setFrame:CGRectMake(20, 20, 80, 20)];
[parentView sizeToFit];

Now create a graphics context and draw parentView into it:

UIGraphicsBeginImageContext([parentView bounds].size);
[[parentView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

You now have a UIImage with the contents of your UIImageView and UITextField to display, save, or send over the network.

pix0r
It is working fine. Thank you for the code. And, DO you have any idea of how to make that text interactive?
Ganesh
A: 

This was a good solution, thanks.

I would like to add that you need to add

#import <QuartzCore/QuartzCore.h>

To your imports so the warnings go away.

Chris