gpt4 book ai didi

ios - 将 UIImage 与 UILabel 合并为一个 UIImage,Swift

转载 作者:行者123 更新时间:2023-11-30 11:23:15 24 4
gpt4 key购买 nike

我正在尝试将 UIImage 与 UILabel“合并”为一个 UIImage,因为我编写了一个函数,除了标签未添加到当前图形上下文之外,一切正常。我将感谢您的帮助!

    func textToImage(drawText: String, inImage: UIImage, atPoint: CGPoint) -> UIImage{

// Setup the image context using the passed image
UIGraphicsBeginImageContext(inImage.size)

// Put the image into a rectangle as large as the original image

inImage.draw(in: CGRect(origin: CGPoint.zero, size: CGSize(width: inImage.size.width, height: inImage.size.height)))

// Create a point within the space that is as bit as the image
let rectPos = CGPoint(x: atPoint.x, y: atPoint.y)
let rectSize = CGSize(width: inImage.size.width, height: inImage.size.height)
let rect = CGRect(origin: rectPos, size: rectSize)

// Draw the text into an image
let label = UILabel()
label.text = drawText
label.textColor = .white
label.font = UIFont(name: "Helvetica Bold", size: 12)!

label.drawText(in: rect)
// Create a new image out of the images we have created
let newImage = UIGraphicsGetImageFromCurrentImageContext()

// End the context now that we have the image we need
UIGraphicsEndImageContext()

//Pass the image back up to the caller
return newImage!
}

最佳答案

您确定要在正确的位置绘制标签吗?是您计算为 CGRect(origin: CGPoint.zero, size: CGSize(width: inImage.size.width, height: inImage.size.height)) 的矩形包含atPoint

关于ios - 将 UIImage 与 UILabel 合并为一个 UIImage,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51056393/

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