Extensions.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // Extensions.swift
  3. // MTImagePicker
  4. //
  5. // Created by Luo on 5/24/16.
  6. // Copyright © 2016 Luo. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. import AssetsLibrary
  11. extension UIScreen {
  12. var compatibleBounds: CGRect {//iOS7 mainScreen bounds 不随设备旋转
  13. var rect = self.bounds
  14. if NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0 {
  15. let orientation = UIApplication.shared.statusBarOrientation
  16. if orientation.isLandscape {
  17. rect.size.width = self.bounds.height
  18. rect.size.height = self.bounds.width
  19. }
  20. }
  21. return rect
  22. }
  23. }
  24. extension ALAsset {
  25. class func getAssetFromUrlSync(lib: ALAssetsLibrary, url: NSURL) -> ALAsset? {
  26. let sema = DispatchSemaphore(value: 0)
  27. var result: ALAsset?
  28. DispatchQueue.global(priority: .default).async {
  29. lib.asset(for: url as URL?, resultBlock: { (asset) in
  30. result = asset
  31. sema.signal()
  32. }, failureBlock: { (_) in
  33. sema.signal()
  34. })
  35. }
  36. sema.wait()
  37. return result
  38. }
  39. class func getLib(failure:() -> Void) -> ALAssetsLibrary? {
  40. let status = ALAssetsLibrary.authorizationStatus()
  41. if status == .authorized || status == .notDetermined {
  42. return ALAssetsLibrary()
  43. } else {
  44. failure()
  45. return nil
  46. }
  47. }
  48. @nonobjc static let lib: ALAssetsLibrary = ALAssetsLibrary()
  49. }
  50. extension Int {
  51. func byteFormat( places: UInt = 2 ) -> String {
  52. if self < 0 {
  53. return ""
  54. } else if self == 0 {
  55. return "0KB"
  56. } else if self < 1024 {
  57. return "1KB"
  58. } else if self < 1024 * 1024 { //KB
  59. return "\(self/1024)KB"
  60. } else if self < 1024 * 1024 * 1024 { //MB
  61. return "\(String(format: "%.\(places)f", Float(self) / 1024 / 1024))MB"
  62. } else {
  63. return "\(String(format: "%.\(places)f", Float(self) / 1024 / 1024 / 1024))GB"
  64. }
  65. }
  66. }
  67. extension Double {
  68. func timeFormat() -> String {
  69. let ticks = Int(self)
  70. let text = String(format: "%d:%02d", ticks/60, ticks%60)
  71. return text
  72. }
  73. }
  74. extension UIView {
  75. func heartbeatsAnimation(duration: Double) {
  76. UIView.animate(withDuration: duration, animations: {
  77. self.transform = CGAffineTransform(scaleX: 1.15, y: 1.15)
  78. }) {
  79. _ in
  80. UIView.animate(withDuration: duration, animations: {
  81. self.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
  82. }) {
  83. _ in
  84. UIView.animate(withDuration: duration) {
  85. self.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
  86. }
  87. }
  88. }
  89. }
  90. }
  91. extension String {
  92. var localized: String {
  93. return NSLocalizedString(self, comment: "")
  94. }
  95. }