gpt4 book ai didi

ios - 将所有 UILabels(具有正确的来源)合并到 UIImage 中

转载 作者:行者123 更新时间:2023-11-29 03:50:08 26 4
gpt4 key购买 nike

因此,我正在开发的应用程序允许用户编写文本,并且该应用程序将该文本转换为单独的 UILabel(一个 UILabel 仅包含文本,另一个 UILabel 与文本宽度相同,但具有透明彩色背景)。我想将所有 uilabels 与后台的 UIImage 组合起来创建一个新的 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不使用我创建的contextref。我不知道如何将文本绘制到上下文中。我认为我也没有正确使用 CGContextSave 和 Restore 。

最佳答案

UIGraphicsBeginImageContextWithOptions(myView.bounds.size, myView.opaque, 0.0);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

其中 myView 是在我们正在创建的图像 (img) 中保存您想要的任何内容的 View 。我没有测试它的标签,但它应该可以正常工作。

关于ios - 将所有 UILabels(具有正确的来源)合并到 UIImage 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17153356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com