根据 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 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)
关于cocoa - NSImage 加载 Windows 光标 .cur 中的热点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48875707/