【问题标题】:Image Generation from TrueType font in Swift/Cocoa在 Swift/Cocoa 中从 TrueType 字体生成图像
【发布时间】:2015-07-02 06:36:17
【问题描述】:

我正在尝试在 Cocoa(不是 UIKit)中使用 Swift 从 ttf 字体生成 NSImage,目前我正在努力创建上下文。

我的基本代码来自这个项目:https://github.com/reeonce/Ionicons.swift 但它是为 UIKit 设计的,所以我尝试为 Cocoa 翻译它。

这是原始代码:

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

这是我目前拥有的:

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

这给了我一个运行时错误,因为我不知道如何初始化上下文:

<Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 168 bytes/row.

对初学者有什么提示吗?谢谢

【问题讨论】:

    标签: macos swift cocoa truetype cgbitmapcontextcreate


    【解决方案1】:

    这是在指定的 Rect 中绘制一些文本的示例

        let context = NSGraphicsContext.currentContext()!.CGContext
    
        //// Text Drawing
        let textRect = NSMakeRect(55, 33, 88, 56)
        let textTextContent = NSString(string: "Hello, World!")
        let textStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
        textStyle.alignment = NSTextAlignment.LeftTextAlignment
    
        let textFontAttributes = [NSFontAttributeName: NSFont(name: "HelveticaNeue-Bold", size: 17)!, NSForegroundColorAttributeName: NSColor.blackColor(), NSParagraphStyleAttributeName: textStyle]
    
        let textTextHeight: CGFloat = textTextContent.boundingRectWithSize(NSMakeSize(textRect.width, CGFloat.infinity), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: textFontAttributes).size.height
        let textTextRect: NSRect = NSMakeRect(textRect.minX, textRect.minY + (textRect.height - textTextHeight) / 2, textRect.width, textTextHeight)
        NSGraphicsContext.saveGraphicsState()
        NSRectClip(textRect)
        textTextContent.drawInRect(NSOffsetRect(textTextRect, 0, 5), withAttributes: textFontAttributes)
        NSGraphicsContext.restoreGraphicsState()
    

    查看 NSGraphicsContext 上的文档了解更多信息 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSGraphicsContext_Class/

    【讨论】:

    • 感谢您的回复。不幸的是,我在第一行得到了fatal error: unexpectedly found nil while unwrapping an Optional value(设置上下文常量)。我错过了什么吗?
    • 糟糕。在 10.10 中,graphicsPort 已折旧。尝试将第一行更改为 let context = NSGraphicsContext.currentContext()!.CGContext 我将修改我的答案以匹配
    • 感谢您的更新,不幸的是,此命令也无法解开可选选项。错误是一样的...
    • 我可以补充一点,我导入了 AppKit 并且我不明白为什么 currentContext() 不能被解包。在此之前是否有特定的事情要做,以指定当前上下文应该是什么?我还尝试了以下let contextPointer = NSGraphicsContext.currentContext()!.graphicsPort; let context = unsafeBitCast(contextPointer, CGContext.self),但也没有运气。这是完整的代码swiftstub.com/126897506/?v=gm
    猜你喜欢
    • 2012-03-22
    • 2011-05-13
    • 2010-10-25
    • 2017-07-04
    • 2016-12-28
    • 2015-11-21
    • 1970-01-01
    • 2012-03-18
    • 2011-07-14
    相关资源
    最近更新 更多