【问题标题】:merging uilabel and uiimageview合并uilabel和uiimageview
【发布时间】:2013-04-16 06:32:19
【问题描述】:

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

到目前为止一切顺利。

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

我试过了

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

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

还有其他建议吗?

【问题讨论】:

  • 您确定已添加 QuartzCore 框架并使用#import 导入它吗?我对您的问题没有具体答案,但这至少应该解决“CAlayer 例如消息是前向声明”错误。
  • 它有助于消除错误,但它没有合并视图:(

标签: ios merge uiimageview uilabel


【解决方案1】:

试试这个:

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

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

【讨论】:

  • 我似乎无法让它工作。没有显示错误。如果我尝试保存图像,那么它会被保存,但 uilabel 中没有文本。我很沮丧,这应该很容易。
  • 嗯,这是我制作的一个小示例项目,应该可以完成我认为您正在尝试完成的工作。查看ViewController.m里面的ViewDidLoad方法github.com/justinrushing/imageview_with_text
  • 嘿贾斯汀,感谢您的帮助,但此解决方案仅保存没有标签的图像视图:(
【解决方案2】:
 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多