123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- //
- // Extensions.swift
- // MTImagePicker
- //
- // Created by Luo on 5/24/16.
- // Copyright © 2016 Luo. All rights reserved.
- //
- import Foundation
- import UIKit
- import AssetsLibrary
- extension UIScreen {
- var compatibleBounds: CGRect {//iOS7 mainScreen bounds 不随设备旋转
- var rect = self.bounds
- if NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0 {
- let orientation = UIApplication.shared.statusBarOrientation
- if orientation.isLandscape {
- rect.size.width = self.bounds.height
- rect.size.height = self.bounds.width
- }
- }
- return rect
- }
- }
- extension ALAsset {
- class func getAssetFromUrlSync(lib: ALAssetsLibrary, url: NSURL) -> ALAsset? {
- let sema = DispatchSemaphore(value: 0)
- var result: ALAsset?
- DispatchQueue.global(priority: .default).async {
- lib.asset(for: url as URL?, resultBlock: { (asset) in
- result = asset
- sema.signal()
- }, failureBlock: { (_) in
- sema.signal()
- })
- }
- sema.wait()
- return result
- }
- class func getLib(failure:() -> Void) -> ALAssetsLibrary? {
- let status = ALAssetsLibrary.authorizationStatus()
- if status == .authorized || status == .notDetermined {
- return ALAssetsLibrary()
- } else {
- failure()
- return nil
- }
- }
- @nonobjc static let lib: ALAssetsLibrary = ALAssetsLibrary()
- }
- extension Int {
- func byteFormat( places: UInt = 2 ) -> String {
- if self < 0 {
- return ""
- } else if self == 0 {
- return "0KB"
- } else if self < 1024 {
- return "1KB"
- } else if self < 1024 * 1024 { //KB
- return "\(self/1024)KB"
- } else if self < 1024 * 1024 * 1024 { //MB
- return "\(String(format: "%.\(places)f", Float(self) / 1024 / 1024))MB"
- } else {
- return "\(String(format: "%.\(places)f", Float(self) / 1024 / 1024 / 1024))GB"
- }
- }
- }
- extension Double {
- func timeFormat() -> String {
- let ticks = Int(self)
- let text = String(format: "%d:%02d", ticks/60, ticks%60)
- return text
- }
- }
- extension UIView {
- func heartbeatsAnimation(duration: Double) {
- UIView.animate(withDuration: duration, animations: {
- self.transform = CGAffineTransform(scaleX: 1.15, y: 1.15)
- }) {
- _ in
- UIView.animate(withDuration: duration, animations: {
- self.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
- }) {
- _ in
- UIView.animate(withDuration: duration) {
- self.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
- }
- }
- }
- }
- }
- extension String {
- var localized: String {
- return NSLocalizedString(self, comment: "")
- }
- }
|