Skip to content

Instantly share code, notes, and snippets.

@maximbilan
Created January 3, 2016 16:33
Show Gist options
  • Save maximbilan/313205c091e0c49c7743 to your computer and use it in GitHub Desktop.
Save maximbilan/313205c091e0c49c7743 to your computer and use it in GitHub Desktop.
import UIKit
public enum DeviceSpecific {
case iPhone
case iPhoneRetina
case iPhone5
case iPhone6
case iPhone6Plus
case iPad
case iPadRetina
case Unknown
}
public extension UIImage {
private class func currentDeviceSpecific() -> DeviceSpecific {
let h = Float(UIScreen.mainScreen().bounds.size.height)
let w = Float(UIScreen.mainScreen().bounds.size.width)
let pixelDimension = Int(fmaxf(h, w))
switch pixelDimension {
case 480:
return UIScreen.mainScreen().scale > 1.0 ? .iPhoneRetina : .iPhone
case 568:
return .iPhone5
case 667:
return .iPhone6
case 736:
return .iPhone6Plus
case 1024:
return UIScreen.mainScreen().scale > 1.0 ? .iPadRetina : .iPad
default:
return .Unknown
}
}
private class func suffixForDevice() -> String {
switch currentDeviceSpecific() {
case .iPhone:
return ""
case .iPhoneRetina:
return "@2x"
case .iPhone5:
return "-568h@2x"
case .iPhone6:
return "-667h@2x"
case .iPhone6Plus:
return "-736h@3x"
case .iPad:
return "~ipad"
case .iPadRetina:
return "~ipad@2x"
case .Unknown:
return ""
}
}
public class func imageForSpecificDevice(imageName: String) -> UIImage? {
var result: UIImage? = nil
let nameWithSuffix = imageName.stringByAppendingString(UIImage.suffixForDevice())
result = UIImage(named: nameWithSuffix)
if result == nil {
result = UIImage(named: imageName)
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment