-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicExtensions.swift
321 lines (271 loc) · 10.2 KB
/
basicExtensions.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import Foundation
import UIKit
import LocalAuthentication
//Extensions
extension Double {
/// Rounds the double to decimal places value
func roundToPlaces(places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(self * divisor) / divisor
}
}
extension Float {
/// Rounds the double to decimal places value
func roundToPlaces(places:Int) -> Float {
let divisor = pow(10.0, Float(places))
return Float(round(self * divisor) / divisor)
}
}
extension String {
var floatValue: Float {
return (self as NSString).floatValue
}
var QRImage:UIImage?{
let data = self.dataUsingEncoding(NSASCIIStringEncoding)
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransformMakeScale(5, 5)
if let output = filter.outputImage?.imageByApplyingTransform(transform) {
return UIImage(CIImage: output)
}
}
return nil
}
func stringUsingEncoding(encoding: NSStringEncoding)->String{
var data = self.dataUsingEncoding(encoding)
return (NSString(data: data!, encoding: encoding)) as! String
}
}
extension Int {
var BarcodeImage:UIImage?{
let data = "\(self)".dataUsingEncoding(NSASCIIStringEncoding)
if let filter = CIFilter(name: "CICode128BarcodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransformMakeScale(5, 5)
if let output = filter.outputImage?.imageByApplyingTransform(transform) {
return UIImage(CIImage: output)
}
}
return nil
}
}
extension String {
func URLEncodedString() -> String? {
var escapedString = self.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
return escapedString
}
static func queryStringFromParameters(parameters: Dictionary<String,String>) -> String? {
if (parameters.count == 0)
{
return nil
}
var queryString : String? = nil
for (key, value) in parameters {
if let encodedKey = key.URLEncodedString() {
if let encodedValue = value.URLEncodedString() {
if queryString == nil
{
queryString = "?"
}
else
{
queryString! += "&"
}
queryString! += encodedKey + "=" + encodedValue
}
}
}
return queryString
}
}
extension UIImage {
private func _resizeWithAspect_doResize(size: CGSize)->UIImage{
if UIScreen.mainScreen().respondsToSelector("scale"){
UIGraphicsBeginImageContextWithOptions(size,false,UIScreen.mainScreen().scale);
}
else
{
UIGraphicsBeginImageContext(size);
}
self.drawInRect(CGRectMake(0, 0, size.width, size.height));
var newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
func resizeImageWithAspect(scaledToMaxWidth width:CGFloat,maxHeight height :CGFloat)->UIImage
{
let oldWidth = self.size.width;
let oldHeight = self.size.height;
let scaleFactor = (oldWidth > oldHeight) ? width / oldWidth : height / oldHeight;
let newHeight = oldHeight * scaleFactor;
let newWidth = oldWidth * scaleFactor;
let newSize = CGSizeMake(newWidth, newHeight);
return self._resizeWithAspect_doResize(newSize);
}
}
extension NSDictionary {
func stringValue()->String{
var data : NSData = NSKeyedArchiver.archivedDataWithRootObject(self)
return NSString(data: data, encoding: NSUTF8StringEncoding) as! String
}
func DictionaryValueForStringKey()->Dictionary<String, AnyObject>{
var swiftDict : Dictionary<String,AnyObject!> = Dictionary<String,AnyObject!>()
for key : AnyObject in self.allKeys {
let stringKey = key as! String
if let keyValue = self.valueForKey(stringKey){
swiftDict[stringKey] = keyValue
}
}
return swiftDict
}
func DictionaryValueForIntKey()->Dictionary<Int, AnyObject>{
var swiftDict : Dictionary<Int,AnyObject!> = Dictionary<Int,AnyObject!>()
for key : AnyObject in self.allKeys {
let stringKey = key as! Int
if let keyValue = self.valueForKey("\(stringKey)"){
swiftDict[stringKey] = keyValue
}
}
return swiftDict
}
}
extension NSMutableDictionary {
override func stringValue()->String{
var ns = self as? NSDictionary
var data : NSData = NSKeyedArchiver.archivedDataWithRootObject(ns!)
return NSString(data: data, encoding: NSUTF8StringEncoding) as! String
}
}
extension CIImage {
var cgimage:CGImage!{
let context:CIContext? = CIContext(options: nil)
if context != nil {
return context!.createCGImage(self, fromRect: self.extent)
}
return nil
}
var uiimage:UIImage{
var ui = UIImage(CGImage: self.cgimage)
return ui
}
var data:NSData{
var cg = self.cgimage
print(cg)
return UIImagePNGRepresentation(cg.uiimage)!
}
}
extension CGImage {
var data:NSData{
return UIImagePNGRepresentation(UIImage(CGImage: self))!
}
var uiimage:UIImage{
return UIImage(CGImage: self)
}
}
extension NSURL {
func openURL(){
UIApplication.sharedApplication().openURL(self)
}
func resizeImage(image: UIImage, withQuality quality: CGInterpolationQuality, rate: CGFloat) -> UIImage {
let width = image.size.width * rate
let height = image.size.height * rate
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), true, 0)
let context = UIGraphicsGetCurrentContext()
CGContextSetInterpolationQuality(context, quality)
image.drawInRect(CGRectMake(0, 0, width, height))
let resized = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resized;
}
var QRImage:UIImage?{
let data = self.absoluteString.dataUsingEncoding(NSUTF8StringEncoding)
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
filter.setValue("L", forKey: "inputCorrectionLevel")
var ret = UIImage()
ret = UIImage(CGImage: filter.outputImage!.cgimage)
ret.drawInRect(filter.outputImage!.extent)
print(filter.outputImage?.data)
let transform = CGAffineTransformMakeScale(5, 5)
if let output = filter.outputImage?.imageByApplyingTransform(transform) {
return output.uiimage
}
}
return nil
}
}
extension NSData {
func stringUsingEncoding(encoding: UInt)->String{
return (NSString(data: self, encoding: NSUTF8StringEncoding) as! String)
}
}
private func UIImageToCIImage(image: UIImage)->CIImage {
return CIImage(image: image)!
}
extension UIImage {
var qrString: String? {
var image = UIImageToCIImage(self)
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyLow])
var decode = ""
let features = detector.featuresInImage(image)
for feature in features as! [CIQRCodeFeature] {
decode = feature.messageString
}
return decode
}
}
func presentXibViewController(actualVC:UIViewController, name:String, animation:Bool, completion:(()->Void)?){
let vc = UIViewController(nibName: "name", bundle: nil)
actualVC.presentViewController(vc, animated: animation, completion: completion)
}
func errorAlert(vc:UIViewController!, error: NSError!, animated:Bool!, handler: ((UIAlertAction) -> Void)?){
let alert = UIAlertController(title: "\(error.code)", message: error.domain, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Ok", style: .Cancel, handler: handler))
vc.presentViewController(alert, animated: animated, completion: nil)
}
extension UIImage{
func getImageFromWeb(_urlString: String)->UIImage {
let url = NSURL(string: _urlString)
let data = try? NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
print(data, String(data))
return UIImage(data: data!!)!
}
}
extension NSDate {
func addMonth(n: Int) -> NSDate {
let cal = NSCalendar.currentCalendar()
return cal.dateByAddingUnit(.Month, value: n, toDate: self, options: .MatchNextTime)!
}
func addDay(n: Int) -> NSDate {
let cal = NSCalendar.currentCalendar()
return cal.dateByAddingUnit(.Day, value: n, toDate: self, options: .MatchNextTime)!
}
func addSecond(n: Int) -> NSDate {
let cal = NSCalendar.currentCalendar()
return cal.dateByAddingUnit(.Second, value: n, toDate: self, options: .MatchNextTime)!
}
func addYear(n: Int) -> NSDate {
let cal = NSCalendar.currentCalendar()
return cal.dateByAddingUnit(.Year, value: n, toDate: self, options: .MatchNextTime)!
}
var month:Int{
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Month], fromDate: self)
return components.month
}
var day:Int{
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Day], fromDate: self)
return components.day
}
var year:Int{
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Year], fromDate: self)
return components.year
}
var second:Int{
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Second], fromDate: self)
return components.second
}
}