将所有UILabel(具有适当的起源)组合成一个UIImage

所以我正在使用的应用程序可以让用户编写文本,应用程序将该文本转换为单独的UILabel(一个UILabel仅包含文本,另一个UILabel与文本宽度相同,但带有透明背景色) 。 我想在背景中将所有的uilabels和UIImage结合起来,创build一个新的UIImage。

到目前为止,我有下面的代码,但它不会产生任何实际的结果,并会爱上任何人对这个问题的帮助:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 416), NO, 0.0); CGContextRef ctx = UIGraphicsGetCurrentContext(); [_imgViewFinal.layer renderInContext:ctx]; CGContextSaveGState(ctx); for (int i = 0; i < _allLabels.count; i++) { UILabel *tempLabelBg = [_allBgs objectAtIndex:i]; CGContextTranslateCTM(ctx, tempLabelBg.frame.origin.x, tempLabelBg.frame.origin.y); CGContextSaveGState(ctx); [tempLabelBg.layer renderInContext:ctx]; CGContextRestoreGState(ctx); UILabel *tempLabelText = [_allLabels objectAtIndex:i]; [tempLabelText drawTextInRect:tempLabelText.frame]; } UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); NSData *imgData = UIImageJPEGRepresentation(image, 1.0); UIImage * imagePNG = [UIImage imageWithData:imgData]; UIGraphicsEndImageContext(); 

*我知道drawTextInRect不使用我创build的contextref。 我不知道如何将文本绘制到上下文中。 我不认为我正在使用CGContextSave和正确还原。

 UIGraphicsBeginImageContextWithOptions(myView.bounds.size, myView.opaque, 0.0); [myView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

其中myView是持有任何你想在图像(IMG)我们正在创build的视图..我没有testing它的标签,但它应该工作正常。