3

我有一个 UITextView 和一个 UIImageView ,我想将它们转换为单个图像以进行共享。

SaveImageView,就是ImageView,我要保存textview的图片。

Textview 可以在屏幕上移动它,所以我决定保存它们的最终位置并将其提供给 SaveImageView。

首先将 UItextView 转换为图像并保存他的位置。

然后,我想将两个 imageview 加入到一个图像中。

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


UIGraphicsBeginImageContext (self.textView.bounds.size);

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

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();


self.SaveTextView.image=resultingImage;

//加入两个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();

问题是我在textview中写的位置与保存的图像不在同一位置,带有文本的图像略有移动,当它应该是我写的确切位置时,找不到错误在哪里.

有什么帮助吗??

4

2 回答 2

2

尝试实现此方法并在 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
于 2013-10-13T08:38:05.637 回答
1

尝试这个...

//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();

享受!

于 2014-07-28T18:48:13.870 回答