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 Foundation | |
import UIKit | |
import ImageIO | |
import MobileCoreServices | |
extension UIImage { | |
static func animatedGif(from images: [UIImage]) { | |
let fileProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]] as CFDictionary | |
let frameProperties: CFDictionary = [kCGImagePropertyGIFDictionary as String: [(kCGImagePropertyGIFDelayTime as String): 1.0]] as CFDictionary | |
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
extension UIImage { | |
func overlayed(with overlay: UIImage) -> UIImage? { | |
defer { | |
UIGraphicsEndImageContext() | |
} | |
UIGraphicsBeginImageContextWithOptions(size, false, scale) | |
self.draw(in: CGRect(origin: .zero, size: size)) | |
overlay.draw(in: CGRect(origin: .zero, size: size)) | |
if let image = UIGraphicsGetImageFromCurrentImageContext() { | |
return image |
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
extension UIView { | |
func embedView(_ view: UIView, inset: UIEdgeInsets = UIEdgeInsets.zero) { | |
let topConstraint = NSLayoutConstraint( | |
item: self, | |
attribute: .top, | |
relatedBy: .equal, | |
toItem: view, | |
attribute: .top, | |
multiplier: 1, | |
constant: inset.top |
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
@IBDesignable class GridView: UIView { | |
var numberOfColumns: Int = 2 | |
var numberOfRows: Int = 2 | |
var lineWidth: CGFloat = 1.0 | |
var lineColor: UIColor = UIColor.white | |
override func draw(_ rect: CGRect) { | |
if let context = UIGraphicsGetCurrentContext() { | |
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
// MARK: - KVO | |
var observedPaths: [String] = [] | |
// Usage - observeKVO(keyPath: #keyPath(camera.inputCamera.whiteBalanceMode)) | |
func observeKVO(keyPath: String) -> Bool { | |
if shouldObserveKVO { | |
if self.classForCoder.automaticallyNotifiesObservers(forKey: keyPath) { | |
observedPaths.append(keyPath) | |
addObserver(self, forKeyPath: keyPath, options: [.old, .new], context: nil) |
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
class SpinView: UIView { | |
private var animating: Bool = false | |
private func spin(with options: UIViewAnimationOptions) { | |
// this spin completes 360 degrees every 2 seconds | |
UIView.animate(withDuration: 0.5, delay: 0, options: options, animations: {() -> Void in | |
self.transform = self.transform.rotated(by: .pi / 2) | |
}, completion: {(_ finished: Bool) -> Void in | |
if finished { | |
if self.animating { |
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
func imageByNormalizingOrientation() -> UIImage { | |
if imageOrientation == .up { | |
return self | |
} | |
UIGraphicsBeginImageContextWithOptions(size, false, scale) | |
draw(in: CGRect(origin: CGPoint.zero, size: size)) | |
let normalizedImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return normalizedImage! | |
} |
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
extension UIView { | |
func blur() { | |
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light) | |
let blurEffectView = UIVisualEffectView(effect: blurEffect) | |
blurEffectView.frame = self.bounds | |
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] | |
self.addSubview(blurEffectView) | |
} | |
func unBlur() { |
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 Foundation | |
protocol ReusableView: class { | |
static var identifier: String { get } | |
} | |
extension ReusableView where Self: UIView { | |
static var identifier: String { | |
return String(describing: self) | |
} |
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 Foundation | |
extension Optional | |
{ | |
@discardableResult | |
func ifSome(_ handler: (Wrapped) -> Void) -> Optional { | |
switch self { | |
case .some(let wrapped): handler(wrapped); return self | |
case .none: return self | |
} |
NewerOlder