3

I have a UITextView and an UIImageView that I want to convert into a single image to share.

SaveImageView, is the ImageView, where I want to save the image of the textview.

The Textview can move it across the screen, so I decide to save their final position and give it to the SaveImageView.

First convert the UItextView in an image and save his position.

Then, I want to join two imageview, into a single image.

self.SaveTextView.frame = self.textView.frame;


UIGraphicsBeginImageContext (self.textView.bounds.size);

[self.textView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();


self.SaveTextView.image=resultingImage;

//join two uiimageview

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);

[self.BaseImageView.image drawInRect:CGRectMake(0, 0, self.BaseImageView.frame.size.width, self.BaseImageView.frame.size.height)];

[self.SaveTextView.image drawInRect:CGRectMake(self.SaveTextView.frame.origin.x, self.SaveTextView.frame.origin.y, self.SaveTextView.frame.size.width, self.SaveTextView.frame.size.height)];


_SaveImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

The problem is that the place where I write in the textview is not in the same position of the saved image, the image with the text is slightly moved, when it should be the exact spot where I have written, not find where is the error.

Any Help ??

user1908661
  • 89
  • 3
  • 9

2 Answers2

2

Try implement this method and call it on superview:

@implementation UIView (ScreenShot)

- (UIImage *)screenShot
{
  UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);
  [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return image;
}

@end
AlKozin
  • 904
  • 8
  • 25
1

Try this...

//Make Label
UILabel *_labelTimeOutName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 428, 32)];
_labelTimeOutName.text = @"Your label text here";
_labelTimeOutName.minimumScaleFactor = 0.5;
_labelTimeOutName.numberOfLines = 1;
_labelTimeOutName.textColor = [UIColor whiteColor];
_labelTimeOutName.backgroundColor = [UIColor clearColor];
_labelTimeOutName.font  = [UIFont fontWithName:@"TrebuchetMS-Bold" size:24];
_labelTimeOutName.textAlignment = NSTextAlignmentCenter;

//add label inside a temp View
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 428, 32)];
tempView.backgroundColor = [UIColor clearColor];
[tempView addSubview:_labelTimeOutName];

//render image
//CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContextWithOptions(tempView.bounds.size, tempView.opaque, 0.0);
// The view to be rendered
[[tempView layer] renderInContext:UIGraphicsGetCurrentContext() ]; //context];
// Get the rendered image
//////////////////////////////////////////////////////////////////////
UIImage *FinalTextIamge = UIGraphicsGetImageFromCurrentImageContext();
//////////////////////////////////////////////////////////////////////
UIGraphicsEndImageContext();

Enjoy!

Sinuhe Huidobro
  • 191
  • 2
  • 7