Created
January 3, 2016 16:33
-
-
Save maximbilan/313205c091e0c49c7743 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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