Skip to content

Instantly share code, notes, and snippets.

@ecanuto
Forked from wmarbut/displayres.swift
Last active September 17, 2015 03:48
Show Gist options
  • Save ecanuto/d5f6b85a8cff13722f0b to your computer and use it in GitHub Desktop.
Save ecanuto/d5f6b85a8cff13722f0b to your computer and use it in GitHub Desktop.
Get all available display resolutions on your machine
//
// main.swift
// Created by grep-awesome on 6/15/15.
//
import Foundation
import CoreGraphics
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")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment