Pritesh Patel Pritesh Patel - 1 year ago 71
Objective-C Question

How can I get all supported screen resolutions on mac OSX Swift

I want a code that

1)grabs my current screen resolution, (solved).

For example : to get current screen display code is :

system_profiler SPDisplaysDataType |grep Resolution

2)grabs all supported resolution as shown in picture below (unsolved).

or any Objective C code will be also useful

enter image description here

Answer Source

Swift code

    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)