根据图像的 Cocoa 绘图指南文档,NSImage 可以加载 Windows 光标 .cur 文件。
但是如何获得 NSCursor 所需的热点- initWithImage:(NSImage *)newImage hotSpot:(NSPoint)point;
?
正如文档中所说,
在 OS X v10.4 及更高版本中,
NSImage
使用 Image I/O 框架支持许多其他文件格式。
因此,让我们获取一个示例光标文件并在 Swift Playground 中进行实验:
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)