Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top