Functions 2.swift 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import UIKit
  2. import AVFoundation
  3. /**
  4. Returns image with a given name from the resource bundle.
  5. - Parameter name: Image name.
  6. - Returns: An image.
  7. */
  8. func imageNamed(_ name: String) -> UIImage {
  9. let cls = BarcodeScannerViewController.self
  10. var bundle = Bundle(for: cls)
  11. let traitCollection = UITraitCollection(displayScale: 3)
  12. if let resourceBundle = bundle.resourcePath.flatMap({ Bundle(path: $0 + "/BarcodeScanner.bundle") }) {
  13. bundle = resourceBundle
  14. }
  15. guard let image = UIImage(named: name, in: bundle, compatibleWith: traitCollection) else {
  16. return UIImage()
  17. }
  18. return image
  19. }
  20. /**
  21. Returns localized string using localization resource bundle.
  22. - Parameter name: Image name.
  23. - Returns: An image.
  24. */
  25. func localizedString(_ key: String) -> String {
  26. if let path = Bundle(for: BarcodeScannerViewController.self).resourcePath,
  27. let resourceBundle = Bundle(path: path + "/Localization.bundle") {
  28. return resourceBundle.localizedString(forKey: key, value: nil, table: "Localizable")
  29. }
  30. return key
  31. }
  32. /// Checks if the app is running in Simulator.
  33. var isSimulatorRunning: Bool = {
  34. #if (arch(i386) || arch(x86_64)) && os(iOS)
  35. return true
  36. #else
  37. return false
  38. #endif
  39. }()