【问题标题】:Hotspot in Windows cursor .cur loaded by NSImage?NSImage加载的Windows光标.cur中的热点?
【发布时间】:2018-02-19 23:28:03
【问题描述】:

根据 Cocoa Drawing Guide documentation for Images,NSImage 可以加载 Windows 光标 .cur 文件。

但是如何获取 NSCursor - initWithImage:(NSImage *)newImage hotSpot:(NSPoint)point; 所需的热点呢?

【问题讨论】:

    标签: cocoa nsimage nscursor


    【解决方案1】:

    正如文档中所说,

    在 OS X v10.4 及更高版本中,NSImage 使用 Image I/O 框架支持许多其他文件格式。

    所以让我们抓住a sample cursor file 并在 Swift 操场上进行实验:

    import Foundation
    import ImageIO
    
    let url = Bundle.main.url(forResource: "BUSY_L", withExtension: "CUR")! as CFURL
    let source = CGImageSourceCreateWithURL(url, nil)!
    print(CGImageSourceCopyPropertiesAtIndex(source, 0, nil)!)
    

    输出:

    {
        ColorModel = RGB;
        Depth = 8;
        HasAlpha = 1;
        IsIndexed = 1;
        PixelHeight = 32;
        PixelWidth = 32;
        ProfileName = "sRGB IEC61966-2.1";
        hotspotX = 16;
        hotspotY = 16;
    }
    

    所以,为了安全获取热点:

    import Foundation
    import ImageIO
    
    if let url = Bundle.main.url(forResource: "BUSY_L", withExtension: "CUR") as CFURL?,
        let source = CGImageSourceCreateWithURL(url, nil),
        let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [String: Any],
        let x = properties["hotspotX"] as? CGFloat,
        let y = properties["hotspotY"] as? CGFloat
    {
        let hotspot = CGPoint(x: x, y: y)
        print(hotspot)
    }
    

    输出:

    (16.0, 16.0)
    

    【讨论】:

    • 感谢您快速而彻底的回答。我应该写到我已经使用了 CGImageSourceCopyPropertiesAtIndex,但是在从 GCImage 移动到 NSImage 时需要另一种方法。那么用initWithContentsOfFile 替换CGImageSourceCreateImageAtIndexinitWithCGImage 是没有意义的。
    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多