1

I did update of Xcode, cocoa pod, alamofire, alamofireimage today,

now I have a red marque on my code about text to image.

I am a beginner with coding.

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

the red marque comme in ligne

text.draw(in: rect, withAttributes: textFontAttributes )

with the message: Cannot convert value of type '[String : Any]' to expected argument type '[NSAttributedStringKey : Any]?'

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Geoff
  • 21
  • 7
  • just change your textFontAttributes type to `[NSAttributedStringKey: Any]` – Leo Dabus Oct 15 '17 at 13:57
  • `let textFontAttributes: [NSAttributedStringKey: Any] = [ .font: textFont, .foregroundColor: textColor]` – Leo Dabus Oct 15 '17 at 13:57
  • Thank you, it's working now. `let textFontAttributes: [NSAttributedStringKey: Any] = [ NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): textFont, NSAttributedStringKey.foregroundColor: textColor, ]` – Geoff Oct 15 '17 at 14:23
  • thats not what I said. Whats wrong with the code I commented above? – Leo Dabus Oct 15 '17 at 14:24
  • Btw you should use String type instead of NSString. And don't force unwrap the result. Change the return type to optional image `UIImage?` – Leo Dabus Oct 15 '17 at 14:34
  • 1
    I try with only add your first answer. now, I change with the second `let textFontAttributes: [NSAttributedStringKey: Any] = [ .font: textFont, .foregroundColor: textColor]` And it's still ok. – Geoff Oct 15 '17 at 14:43
  • check my post. There are some other issues that you should fix in your code – Leo Dabus Oct 15 '17 at 14:48
  • 1
    Thank you very much. I had to change all NSSTring by String on the page. I have probably did same mistake in other page. I don't really understand what defer do and how to use it. but it's working well – Geoff Oct 15 '17 at 14:58
  • the defer only executes when the method finishes. you are welcome – Leo Dabus Oct 15 '17 at 15:00

1 Answers1

0

There are a few issues with your code. First don't use NSString, Swift native string type is String. Second you need to specify your textFontAttributes type as [NSAttributedStringKey: Any] and don't force unwrap the result. Change the return type to optional image UIImage? You can also use defer to end graphics image context when your method finishes.

func textToImage(drawText text: String, 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)
    defer { UIGraphicsEndImageContext() }
    let textFontAttributes: [NSAttributedStringKey: Any] = [.font: textFont, .foregroundColor: textColor]
    image.draw(in: CGRect(origin: .zero, size: image.size))
    let rect = CGRect(origin: point, size: image.size)
    text.draw(in: rect, withAttributes: textFontAttributes)
    return UIGraphicsGetImageFromCurrentImageContext()
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571