@@ -10,17 +10,30 @@ import UIKit
10
10
import ImageIO
11
11
12
12
extension UIImage {
13
-
14
- public class func animatedImageWithData( data: NSData ) -> UIImage ? {
15
- guard let source = CGImageSourceCreateWithData ( data, nil ) else { return nil }
16
-
13
+
14
+ public class func gifWithData( data: NSData ) -> UIImage ? {
15
+ guard let source = CGImageSourceCreateWithData ( data, nil ) else {
16
+ print ( " SwiftGif: Source for the image does not exist " )
17
+ return nil
18
+ }
17
19
return UIImage . animatedImageWithSource ( source)
18
20
}
19
-
20
- class func delayForImageAtIndex( index: Int , source: CGImageSource ! )
21
- -> Double {
21
+
22
+ public class func gifWithName( name: String ) -> UIImage ? {
23
+ guard let bundleURL = NSBundle . mainBundle ( ) . URLForResource ( name, withExtension: " gif " ) else {
24
+ print ( " SwiftGif: This image named \" \( name) \" does not exist " )
25
+ return nil
26
+ }
27
+ guard let imageData = NSData ( contentsOfURL: bundleURL) else {
28
+ print ( " SwiftGif: Cannot turn image named \" \( name) \" into NSData " )
29
+ return nil
30
+ }
31
+ return gifWithData ( imageData)
32
+ }
33
+
34
+ class func delayForImageAtIndex( index: Int , source: CGImageSource ! ) -> Double {
22
35
var delay = 0.1
23
-
36
+
24
37
// Get dictionaries
25
38
let cfProperties = CGImageSourceCopyPropertiesAtIndex ( source, index, nil )
26
39
let gifProperties : CFDictionaryRef = unsafeBitCast (
@@ -37,17 +50,16 @@ extension UIImage {
37
50
delayObject = unsafeBitCast ( CFDictionaryGetValue ( gifProperties,
38
51
unsafeAddressOf ( kCGImagePropertyGIFDelayTime) ) , AnyObject . self)
39
52
}
40
-
53
+
41
54
delay = delayObject as! Double
42
-
55
+
43
56
if delay < 0.1 {
44
57
delay = 0.1 // Make sure they're not too fast
45
58
}
46
-
47
-
59
+
48
60
return delay
49
61
}
50
-
62
+
51
63
class func gcdForPair( var a: Int ? , var _ b: Int ? ) -> Int {
52
64
// Check if one of them is nil
53
65
if b == nil || a == nil {
@@ -59,19 +71,19 @@ extension UIImage {
59
71
return 0
60
72
}
61
73
}
62
-
74
+
63
75
// Swap for modulo
64
76
if a < b {
65
77
let c = a
66
78
a = b
67
79
b = c
68
80
}
69
-
81
+
70
82
// Get greatest common divisor
71
83
var rest : Int
72
84
while true {
73
85
rest = a! % b!
74
-
86
+
75
87
if rest == 0 {
76
88
return b! // Found it
77
89
} else {
@@ -80,70 +92,70 @@ extension UIImage {
80
92
}
81
93
}
82
94
}
83
-
95
+
84
96
class func gcdForArray( array: Array < Int > ) -> Int {
85
97
if array. isEmpty {
86
98
return 1
87
99
}
88
-
100
+
89
101
var gcd = array [ 0 ]
90
-
102
+
91
103
for val in array {
92
104
gcd = UIImage . gcdForPair ( val, gcd)
93
105
}
94
-
106
+
95
107
return gcd
96
108
}
97
-
98
- public class func animatedImageWithSource( source: CGImageSource ) -> UIImage ? {
109
+
110
+ class func animatedImageWithSource( source: CGImageSource ) -> UIImage ? {
99
111
let count = CGImageSourceGetCount ( source)
100
112
var images = [ CGImageRef] ( )
101
113
var delays = [ Int] ( )
102
-
114
+
103
115
// Fill arrays
104
116
for i in 0 ..< count {
105
117
// Add image
106
118
if let image = CGImageSourceCreateImageAtIndex ( source, i, nil ) {
107
119
images. append ( image)
108
120
}
109
-
121
+
110
122
// At it's delay in cs
111
123
let delaySeconds = UIImage . delayForImageAtIndex ( Int ( i) ,
112
124
source: source)
113
125
delays. append ( Int ( delaySeconds * 1000.0 ) ) // Seconds to ms
114
126
}
115
-
127
+
116
128
// Calculate full duration
117
129
let duration : Int = {
118
130
var sum = 0
119
-
131
+
120
132
for val : Int in delays {
121
133
sum += val
122
134
}
123
-
135
+
124
136
return sum
125
137
} ( )
126
-
138
+
127
139
// Get frames
128
140
let gcd = gcdForArray ( delays)
129
141
var frames = [ UIImage] ( )
130
-
142
+
131
143
var frame : UIImage
132
144
var frameCount : Int
133
145
for i in 0 ..< count {
134
146
frame = UIImage ( CGImage: images [ Int ( i) ] )
135
147
frameCount = Int ( delays [ Int ( i) ] / gcd)
136
-
148
+
137
149
for _ in 0 ..< frameCount {
138
150
frames. append ( frame)
139
151
}
140
152
}
141
-
153
+
142
154
// Heyhey
143
155
let animation = UIImage . animatedImageWithImages ( frames,
144
156
duration: Double ( duration) / 1000.0 )
145
-
157
+
146
158
return animation
147
159
}
148
-
160
+
149
161
}
0 commit comments