1

我从相册中挑选了一张图片并将其放入UIImageView名为 imageView 的图片中,然后在图片上添加了一些文本,UITextView用作输入源,并UILabel在 uiimageview 顶部添加了一些文本以显示文本。

到目前为止,一切都很好。

现在我正在尝试合并标签和图像视图,以便用它做其他事情,但我没有让它工作。我已经尝试了一些针对类似问题的解决方案,但它们对我不起作用。

我试过这个

UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0.0); //retina res
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
[_displaylabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

它告诉我收到的类型“CAlayer例如消息是前向声明”。

还有其他建议吗?

4

2 回答 2

0
 UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
 UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imgView.frame.size.width, imgView.frame.size.height)];
    [tempView addSubview:imgView];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(tempView.frame.size.width/2, 0, 200, tempView.frame.size.height/2)];
[label setText:@"Hello... You there"];
[label setTextColor:[UIColor blackColor]];
[label setTextAlignment:NSTextAlignmentLeft];
[label setBackgroundColor:[UIColor clearColor]];
[tempView addSubview:label];


 UIGraphicsBeginImageContext(tempView.bounds.size);
    [tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
于 2014-12-15T07:45:00.143 回答
0

尝试这个:

UIGraphicsBeginImageContextWithOptions(_imageView.bounds.size, NO, 0.0); //retina res
[self.imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

确保 UILabel 是 UIImageView 的子视图,以便渲染层将包括所有子层(包括 UILabel)。好像你忘了包括 UIGraphicsEndImageContext(); 也是,这可能是问题所在

于 2013-04-16T07:05:18.067 回答