【发布时间】:2018-02-19 23:28:03
【问题描述】:
根据 Cocoa Drawing Guide documentation for Images,NSImage 可以加载 Windows 光标 .cur 文件。
但是如何获取 NSCursor - initWithImage:(NSImage *)newImage hotSpot:(NSPoint)point; 所需的热点呢?
【问题讨论】:
根据 Cocoa Drawing Guide documentation for Images,NSImage 可以加载 Windows 光标 .cur 文件。
但是如何获取 NSCursor - initWithImage:(NSImage *)newImage hotSpot:(NSPoint)point; 所需的热点呢?
【问题讨论】:
正如文档中所说,
在 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)
【讨论】:
GCImage 移动到 NSImage 时需要另一种方法。那么用initWithContentsOfFile 替换CGImageSourceCreateImageAtIndex 和initWithCGImage 是没有意义的。