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

时间:2018-02-19 23:28:03

标签: cocoa nsimage nscursor

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

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

1 个答案:

答案 0 :(得分: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)