我环顾四周,想不出如何获取文本,将其叠加到一张图像上,然后将两者合并成一个单独的UIImage.

我已经用尽了谷歌使用的搜索词,我能想到的,所以如果任何人有一个解决方案或至少一个提示,他们可以指向它将不胜感激.

推荐答案

好的...我想通了:

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

    // Setup the font specific variables
    var textColor = UIColor.whiteColor()
    var textFont = UIFont(name: "Helvetica Bold", size: 12)!

    // Setup the image context using the passed image
    let scale = UIScreen.mainScreen().scale
    UIGraphicsBeginImageContextWithOptions(inImage.size, false, scale)

    // Setup the font attributes that will be later used to dictate how the text should be drawn
    let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,
    ]

    // Put the image into a rectangle as large as the original image
    inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))

    // Create a point within the space that is as bit as the image
    var rect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height)

    // Draw the text into an image
    drawText.drawInRect(rect, withAttributes: textFontAttributes)

    // Create a new image out of the images we have created
    var newImage = UIGraphicsGetImageFromCurrentImageContext()

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

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

}

要调用它,只需传入一个图像:

textToImage("000", inImage: UIImage(named:"thisImage.png")!, atPoint: CGPointMake(20, 20))

以下链接帮助我弄清楚了这一点:

Swift - Drawing text with drawInRect:withAttributes:

How to write text on image in Objective-C (iOS)?

最初的目标是创建一个动态图像,我可以在AnnotaionView中使用它,比如在 map 上的某个给定位置放置一个价格,这非常适合它.希望这能帮助try 做同样事情的人.

For Swift 3:

 func textToImage(drawText text: NSString, 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 = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,
        ] as [String : 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!
 }

For Swift 4:

 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!
 }

For Swift 5:

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 = [
        NSAttributedString.Key.font: textFont,
        NSAttributedString.Key.foregroundColor: textColor,
        ] as [NSAttributedString.Key : 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!
}

Ios相关问答推荐

在选项卡内列出导航内视图断开导航标题的动画

RxSwift:如何使用另外两个可观测对象(其中一个依赖于另一个)创建可观测对象?

在嵌套的DISPATCH_AFTER块中正确使用strong自/弱自

在flutter中使用flatter_screenutil这样的软件包的目的是什么?

如何在expo-react native中从ios平台中排除包

许多文本字段的性能问题

BezierPath 中绘制图像形状轮廓的算法(Canny 边缘检测器)

创建方案时运行 pod install 时出错

Flutter IOS 构建错误 - 在签名和功能编辑器中 Select 一个开发团队.

VStack 显示错误实例方法 'sheet(item:onDismiss:content:)' 要求 'Int' 符合 'Identifiable'

无法初始化 FirebaseApp - Flutter

如何在不支持并发的自动关闭中修复'async'调用?

如何识别 SwiftUI 中点击的按钮 ID 以使该按钮动画化?

在使用 Github Actions 时,我面临权限被拒绝错误

在 iOS 编程中使用 Storyboard 代替 xib 文件有什么好处?

如何在 UIActivityViewController 中设置邮件主题?

是否可以在没有 Mac 的情况下为 iOS 制作 PhoneGap 应用程序?

动画 UILabel 字体大小更改

如何将自己项目中的类导入 Playground

你如何以编程方式从视图控制器中画一条线?