iOS原生、Android 原生, flutter 三种方式给照片流添加文字(水印)

编程入门 行业动态 更新时间:2024-10-28 00:19:25

iOS原生、Android 原生, flutter <a href=https://www.elefans.com/category/jswz/34/1770022.html style=三种方式给照片流添加文字(水印)"/>

iOS原生、Android 原生, flutter 三种方式给照片流添加文字(水印)

效果图:三中代码实现的效果差不多

Swift:代码

import UIKitclass ImageWatermarking: NSObject {static func textToImage(drawText text: String, inImage initImage: UIImage, atPoint point: CGPoint) -> UIImage {let textColor = UIColor.whitelet textFont = UIFont(name:"Helvetica", size: 26)!// 新建底部拼接区域let rect: CGRect = CGRect(x: 0, y: 0, width: initImage.size.width, height: 220)UIGraphicsBeginImageContext(rect.size)let context: CGContext = UIGraphicsGetCurrentContext()!context.setFillColor(UIColor.kHexRGBA(0x000000,0.3).cgColor)context.fill(rect)let imagebottom = UIGraphicsGetImageFromCurrentImageContext()UIGraphicsGetCurrentContext()let style = NSMutableParagraphStyle()style.lineSpacing = 6style.lineBreakMode = NSLineBreakMode.byTruncatingTail// 在底部拼接区域添加文字let textFontAttributes = [NSAttributedString.Key.font: textFont,NSAttributedString.Key.foregroundColor: textColor,NSAttributedString.Key.paragraphStyle: style] as [NSAttributedString.Key : Any]imagebottom!.draw(in: CGRect(origin: CGPoint.zero, size: imagebottom!.size))let rectText = CGRect(origin: point, size: initImage.size)text.draw(in: rectText, withAttributes: textFontAttributes)let newTextImage = UIGraphicsGetImageFromCurrentImageContext()UIGraphicsEndImageContext()// 拼接原图和新建的底部区域let topImage = initImagelet bottomImage = newTextImagelet allSize = CGSize(width: topImage.size.width, height: topImage.size.height )UIGraphicsBeginImageContext(allSize)let areaSizeTop = CGRect(x: 0, y: 0, width: topImage.size.width, height: topImage.size.height)topImage.draw(in: areaSizeTop)let areaSizeBottom = CGRect(x: 0, y: areaSizeTop.size.height-220, width: bottomImage!.size.width, height: bottomImage!.size.height)bottomImage!.draw(in: areaSizeBottom)let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!UIGraphicsEndImageContext()return newImage}}

Swift 调用:

        
let contentStr :String =  currentTime &

更多推荐

iOS原生、Android 原生, flutter 三种方式给照片流添加文字(水印)

本文发布于:2023-12-07 02:02:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1669739.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:三种   水印   文字   方式   照片

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!