I'm trying to generate an NSImage from a ttf font using Swift in Cocoa (not UIKit) and I'm struggling with the context creation at the moment.
My base code came from this project: https://github.com/reeonce/Ionicons.swift but it's designed for UIKit, so I tried translating it for Cocoa.
Here's the original code:
extension UIImage {
public class func imageWithIonIcon(icon: Ionicons, height: CGFloat, color: UIColor) -> UIImage {
let font = UIFont(name: "ionicons", size: height)!
let iconSize = (icon.rawValue as NSString).sizeWithAttributes([NSFontAttributeName: font])
UIGraphicsBeginImageContextWithOptions(iconSize, false, 0.0)
(icon.rawValue as NSString).drawAtPoint(CGPointZero, withAttributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color])
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
and here is what I have at the moment:
extension NSImage {
public class func imageWithIonIcon(icon: Ionicons, height: CGFloat, color: NSColor) -> NSImage? {
if let font = NSFont(name: "Ionicons", size: height) {
let iconSize = (icon.rawValue as NSString).sizeWithAttributes([NSFontAttributeName: font])
let context = CGBitmapContextCreate(nil, Int(iconSize.width), Int(iconSize.height), 8, Int(iconSize.width)*8, CGColorSpaceCreateDeviceRGB(), nil)
(icon.rawValue as NSString).drawAtPoint(CGPointZero, withAttributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color])
let image = NSImage(CGImage: CGBitmapContextCreateImage(context), size: iconSize)
return image
}
return nil
}
}
This gives me a runtime error because I have no idea how to initialize the context:
<Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 168 bytes/row.
Any hint for a beginner? Thanks