gpt4 book ai didi

ios - 将正确定位的图层添加到上下文

转载 作者:行者123 更新时间:2023-11-29 01:57:49 24 4
gpt4 key购买 nike

我正在尝试将两个 CALayer 从 UIImageView 和 UITextField 添加到上下文,然后使用 UIGraphicsGetImageFromCurrentImageContext() 从上下文创建图像,但是,我似乎无法定位文本字段正确。我希望生成的图像与图像和文本字段最初出现的 View 相匹配。相反,文本字段显示为半隐藏在图像顶部之外。目前,我正在尝试这样的事情(这是错误的):

UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, false, 0.0)
let ctx = UIGraphicsGetCurrentContext()
let ht = self.imageView.bounds.size.height
let wdth = self.imageView.bounds.size.width
self.imageView.layer.renderInContext(ctx)

CGContextTranslateCTM(ctx, self.imageView.center.x, self.imageView.center.y)
CGContextConcatCTM(ctx, self.imageView.transform)
CGContextTranslateCTM(ctx, -wdth * self.imageView.layer.anchorPoint.x, -ht * self.imageView.layer.anchorPoint.y)
// Set points for text label here, then...
self.textLabelTop.layer.renderInContext(ctx)

let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage

我理解 CGContextTranslateCTM()CTContextConcatCTM() 必须将原始坐标系应用到上下文,然后放置图层,但我不确定如何正确地做到这一点。有人可以提供正确技术的示例吗?

更新:这看起来像是一个 hack,但不是将点转换为上下文,如果我只是在编辑之后和 UIGraphicsGetImageFromCurrentImageContext( ),事情似乎可行,包括放置第二个文本字段:

self.imageView.addSubview(self.textLabelTop)
self.imageView.addSubview(self.textLabelBottom)
UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, false, 0.0)
let ctx = UIGraphicsGetCurrentContext()
self.imageView.layer.renderInContext(ctx)
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage

最佳答案

您应该了解 convertPoint:toView: 的可能性。试试这个:

UIGraphicsBeginImageContextWithOptions(self.imageView.frame.size, false, 0.0)
let ctx = UIGraphicsGetCurrentContext()
self.imageView.layer.renderInContext(ctx)

let point = self.textLabelTop.superview!.convertPoint(self.textLabelTop.frame.origin, toView: self.imageView)
CGContextTranslateCTM(ctx, point.x, point.y)
self.textLabelTop.layer.renderInContext(ctx)

let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage

关于ios - 将正确定位的图层添加到上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30657210/

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