我想要一个代码
1)抓住我当前的屏幕分辨率,(已解决)。
例如:获取当前屏幕显示代码为:
system_profiler SPDisplaysDataType | grep Resolution
2)抓取所有支持的分辨率,如下图所示(未解决)。
或任何Objective C代码也很有用
答案 0 :(得分:1)
Swift代码
var displayConfig: CGDisplayConfigRef = nil
let mainDisplayID = CGMainDisplayID()
var displayMode = CGDisplayCopyDisplayMode(mainDisplayID).takeRetainedValue()
var width = CGDisplayModeGetWidth(displayMode)
var height = CGDisplayModeGetHeight(displayMode)
print("current size: \(width)x\(height)\n")
print("available sizes:\n")
var modes = CGDisplayCopyAllDisplayModes(mainDisplayID, nil).takeRetainedValue()
let modesCount = CFArrayGetCount(modes) - 1
for i in 0...modesCount {
var mode: CGDisplayModeRef = unsafeBitCast(CFArrayGetValueAtIndex(modes, i), CGDisplayModeRef.self)
var width = CGDisplayModeGetWidth(mode)
var height = CGDisplayModeGetHeight(mode)
print("\t\(width)x\(height)\n")
答案 1 :(得分:1)
Xcode 8•Swift 3
struct Display {
static var mode: CGDisplayMode? { return CGDisplayCopyDisplayMode(CGMainDisplayID()) }
static var modes: [CGDisplayMode] {
var result: [CGDisplayMode] = []
let modes = CGDisplayCopyAllDisplayModes(CGMainDisplayID(), nil).unsafelyUnwrapped
(0..<CFArrayGetCount(modes)).forEach({result.append(unsafeBitCast(CFArrayGetValueAtIndex(modes, $0), to: CGDisplayMode.self))})
return result
}
}
extension CGDisplayMode {
var resolution: String { return String(width) + " x " + String(height) }
}
用法:
if let resolution = Display.mode?.resolution {
print("Resolution:", resolution)
}
for mode in Display.modes {
print(mode.resolution)
}