栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在iOS Swift中向图像添加文本?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在iOS Swift中向图像添加文本?

好吧…我知道了:

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))

最初的目标是创建一个动态图像,我可以在其中使用该图像,

AnnotaionView
例如在地图上的给定位置标价,这很适合。希望这对尝试做同样事情的人有所帮助。

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

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/368393.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号