0

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.

初心者向けのヒントはありますか?ありがとう

4

1 に答える 1

0

指定した 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()

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSGraphicsContext_Class/の詳細については、NSGraphicsContext のドキュメントを ご覧ください。

于 2015-07-02T07:08:57.530 に答える