NSImage 加载的 Windows 游标 .cur 中的热点?

Hotspot in Windows cursor .cur loaded by NSImage?

根据 Cocoa 图像绘图指南 documentation,NSImage 可以加载 Windows 光标 .cur 文件。

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

如文档所述,

In OS X v10.4 and later, NSImage supports many additional file formats using the Image I/O framework.

所以让我们抓住 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)