【发布时间】:2021-12-11 18:26:50
【问题描述】:
我有这样的图像:
但我想在这张图片下添加一些文字,并像新的 UIImage 一样获取它,例如:
据我所知,我应该创建一个新图像。然后将我的图像放入其中并添加文本。 我该怎么做?
我尝试使用类似的东西,但无法将我的文字放在图像下方
func textToImage(drawText text: String, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
let textColor = UIColor.white
let textFont = UIFont(name: "Helvetica Bold", size: 12)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
let textFontAttributes = [
NSAttributedStringKey.font: textFont,
NSAttributedStringKey.foregroundColor: textColor,
] as [NSAttributedStringKey : Any]
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
let rect = CGRect(origin: point, size: image.size)
text.draw(in: rect, withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
PS 我的图片背景很清晰。红色是另一个视图的背景。
【问题讨论】:
-
SwiftUI 还是 UIKit?
-
对不起,我用的是 UIKit
-
你肯定尝试过 something。 SO 不是免费的代码编写网站。如果不表现出至少一些(研究)努力,您就不太可能在这里收到答案。
-
我添加了我的代码
标签: ios swift uikit uiimage core-graphics