-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.swift
103 lines (82 loc) · 3.24 KB
/
Extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// Extensions.swift
// gameofchats
//
// Created by Brian Voong on 7/5/16.
// Copyright © 2016 letsbuildthatapp. All rights reserved.
//
import UIKit
import IQMediaPickerController
let imageCache = NSCache<NSString, AnyObject>()
extension UIImageView {
func loadImageUsingCacheWithUrlString(_ urlString: String, _ heightAnchor: NSLayoutDimension? = nil, _ spinner: UIActivityIndicatorView) {
self.image = nil
//check cache for image first
if let cachedImage = imageCache.object(forKey: urlString as NSString) as? UIImage {
self.image = cachedImage
return
}
//otherwise fire off a new download
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
//download hit an error so lets return out
if let error = error {
print(error)
return
}
DispatchQueue.main.async(execute: {
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
spinner.hidesWhenStopped = true
spinner.stopAnimating()
self.image = downloadedImage
}
})
}).resume()
}
func generateThumbImage(url : URL) -> UIImage?{
// let asset = AVAsset(url: url)
// let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
// assetImgGenerate.appliesPreferredTrackTransform = true
// let time = CMTimeMake(1, 30)
// let img = try? assetImgGenerate.copyCGImage(at: time, actualTime: nil)
//
// guard let cgImage = img else { return nil }
//
// let frameImg = UIImage(cgImage: cgImage)
// return frameImg
return UIImage()
}
}
extension UIView
{
func addConstraintsWithForamt (format: String, views: UIView ...) {
var viewsDict = [String: UIView]();
for (ind, view) in views.enumerated() {
let key = "v\(ind)"
viewsDict[key] = view
view.translatesAutoresizingMaskIntoConstraints = false
}
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDict))
}
}
extension UIColor
{
static func rgb(_ red: CGFloat, _ green: CGFloat, _ blue: CGFloat, _ alph: CGFloat = 1) -> UIColor
{
return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: alph)
}
}
extension UIViewController {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
}