1
我今天更新了Xcode,可可豆荚,alamofire,alamofireimage,Swift text to image issue。无法将类型'[String:Any]'的值转换为期望的参数类型'[NSAttributedStringKey:Any]?'
现在我在我的关于文字图像的代码上有一个红色的标记。
我是一个编码的初学者。
func textToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
let textColor = UIColor.red
let textFont = UIFont(name: "Arial Rounded MT Bold", size: 24)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
let textFontAttributes = [
NSAttributedStringKey.font.rawValue: textFont,
NSAttributedStringKey.foregroundColor: 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!
}
红色劳斯莱斯COMME在LIGNE
text.draw(in: rect, withAttributes: textFontAttributes)
与消息:无法转换类型 '[字符串:任何]' 的值与预期的参数类型'[NSAttributedStringKey:任何]?
只是改变你的textFontAttributes类型'[NSAttributedStringKey:任何]'' –
让textFontAttributes:[NSAttributedStringKey:任何] = [ .font:TEXTFONT, .foregroundColor:文字颜色]' –
谢谢你,它的工作现在。 '让textFontAttributes:[NSAttributedStringKey:任何] = [ NSAttributedStringKey(rawValue:NSAttributedStringKey.font.rawValue):TEXTFONT, NSAttributedStringKey.foregroundColor:文字颜色, ]' – Geoff