cocoa - NSImage 加载 Windows 光标 .cur 中的热点?

标签 cocoa nsimage nscursor

根据 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/

相关文章:

cocoa - 在我的应用程序主菜单中设置项目标题?

cocoa 将图像合成在一起

xcode - resetCursorRects 不起作用(新光标仅在鼠标进入时闪烁)

Swift - 在 NSTableView 中使用复选框获取行索引

objective-c - AVAudioUnit(渲染)回调

objective-c - "Append"拖动期间添加/加号图标到 NSImage

objective-c - NSCursor 总是重置为 Arrow

macos - 调用 [NSCursor set] 是否会对性能造成影响?

iphone - 如何在 Xcode 中批量抑制编译器警告

objective-c - 使用或不使用 NSAffineTransform 旋转 NSImage