Cocoa Windows游标中的热点。NSImage加载的cur?
根据Cocoa图像绘图指南,NSImage可以加载Windows游标.cur文件Cocoa Windows游标中的热点。NSImage加载的cur?,cocoa,nsimage,nscursor,Cocoa,Nsimage,Nscursor,根据Cocoa图像绘图指南,NSImage可以加载Windows游标.cur文件 但是如何获得NSCursor-initWithImage:(NSImage*)新图像热点:(NSPoint)点所需的热点?正如文档中所述 在OS X v10.4及更高版本中,NSImage使用映像I/O框架支持许多其他文件格式 所以,让我们在一个快速的操场上抓取和实验: import Foundation import ImageIO let url = Bundle.main.url(forResource:
但是如何获得NSCursor
-initWithImage:(NSImage*)新图像热点:(NSPoint)点所需的热点代码>?正如文档中所述
在OS X v10.4及更高版本中,NSImage
使用映像I/O框架支持许多其他文件格式
所以,让我们在一个快速的操场上抓取和实验:
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;
}
(16.0, 16.0)
因此,为了安全地获取热点:
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)
}
输出:
{
ColorModel = RGB;
Depth = 8;
HasAlpha = 1;
IsIndexed = 1;
PixelHeight = 32;
PixelWidth = 32;
ProfileName = "sRGB IEC61966-2.1";
hotspotX = 16;
hotspotY = 16;
}
(16.0, 16.0)
谢谢你快速而彻底的回答。我应该写下我已经使用了CGImageSourceCopyPropertiesAtIndex,但在从GCImage
移动到NSImage
时,我希望使用另一种方法。因此,将CGImageSourceCreateImageAtIndex
和initWithCGImage
替换为initWithContentsOfFile
没有意义。