Skip to content
This repository was archived by the owner on Jan 14, 2021. It is now read-only.

Commit ad6fdfc

Browse files
committed
Merge branch '1.5-development'
* 1.5-development: Update to recommended settings Add badges Better markup in README.md added public statements Update README.md Update RootViewController.swift Update UIImage+Gif.swift Add build status badge Update .travis.yml Added swift2 update check message tracker
2 parents fd935e6 + f5e936c commit ad6fdfc

File tree

9 files changed

+86
-61
lines changed

9 files changed

+86
-61
lines changed

.travis.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
language: swift
1+
language: objective-c
2+
osx_image: xcode7
23

4+
script:
5+
- xctool
6+
-project SwiftGif.xcodeproj -scheme SwiftGif
7+
-sdk iphonesimulator build test
8+
CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY=""

README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SwiftGif
1+
# SwiftGif [![Swift 2.0](https://img.shields.io/badge/Swift-2.0-orange.svg?style=flat)](https://developer.apple.com/swift/) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![CocoaPods](https://img.shields.io/cocoapods/v/SwiftGifOrigin.svg)](http://cocoadocs.org/docsets/SwiftGifOrigin) [![License MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat)](https://github.com/Carthage/Carthage) [![Build Status](https://travis-ci.org/bahlo/SwiftGif.svg?branch=master)](https://travis-ci.org/bahlo/SwiftGif)
22

33
A small `UIImage` extension with gif support.
44

@@ -7,12 +7,11 @@ A small `UIImage` extension with gif support.
77
## Usage
88
Import the `Gif.swift` in your project and do the following:
99
```swift
10-
// jeremy.gif
11-
var url = NSBundle.mainBundle().URLForResource("jeremy", withExtension: "gif")
12-
var imageData = NSData(contentsOfURL: url)
13-
1410
// Returns an animated UIImage
15-
UIImage.animatedImageWithData(imageData)
11+
let jeremyGif = UIImage.gifWithName("jeremy")
12+
13+
// Use the UIImage in your UIImageView
14+
let imageView = UIImageView(image: jeremyGif)
1615
```
1716

1817
## How does it work?

SwiftGif.xcodeproj/project.pbxproj

+7-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@
155155
14A76430194EFBB800A74B1F /* Project object */ = {
156156
isa = PBXProject;
157157
attributes = {
158-
LastUpgradeCheck = 0600;
158+
LastSwiftUpdateCheck = 0700;
159+
LastUpgradeCheck = 0700;
159160
ORGANIZATIONNAME = "Arne Bahlo";
160161
TargetAttributes = {
161162
14A76437194EFBB800A74B1F = {
@@ -257,6 +258,7 @@
257258
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
258259
COPY_PHASE_STRIP = NO;
259260
ENABLE_STRICT_OBJC_MSGSEND = YES;
261+
ENABLE_TESTABILITY = YES;
260262
GCC_C_LANGUAGE_STANDARD = gnu99;
261263
GCC_DYNAMIC_NO_PIC = NO;
262264
GCC_OPTIMIZATION_LEVEL = 0;
@@ -321,6 +323,7 @@
321323
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
322324
INFOPLIST_FILE = "$(SRCROOT)/SwiftGifDemo/Info.plist";
323325
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
326+
PRODUCT_BUNDLE_IDENTIFIER = "me.arne.${PRODUCT_NAME:rfc1034identifier}";
324327
PRODUCT_NAME = "$(TARGET_NAME)";
325328
};
326329
name = Debug;
@@ -332,6 +335,7 @@
332335
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
333336
INFOPLIST_FILE = "$(SRCROOT)/SwiftGifDemo/Info.plist";
334337
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
338+
PRODUCT_BUNDLE_IDENTIFIER = "me.arne.${PRODUCT_NAME:rfc1034identifier}";
335339
PRODUCT_NAME = "$(TARGET_NAME)";
336340
};
337341
name = Release;
@@ -351,6 +355,7 @@
351355
INFOPLIST_FILE = SwiftGifTests/Info.plist;
352356
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
353357
METAL_ENABLE_DEBUG_INFO = YES;
358+
PRODUCT_BUNDLE_IDENTIFIER = "me.arne.${PRODUCT_NAME:rfc1034identifier}";
354359
PRODUCT_NAME = "$(TARGET_NAME)";
355360
TEST_HOST = "$(BUNDLE_LOADER)";
356361
};
@@ -367,6 +372,7 @@
367372
INFOPLIST_FILE = SwiftGifTests/Info.plist;
368373
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
369374
METAL_ENABLE_DEBUG_INFO = NO;
375+
PRODUCT_BUNDLE_IDENTIFIER = "me.arne.${PRODUCT_NAME:rfc1034identifier}";
370376
PRODUCT_NAME = "$(TARGET_NAME)";
371377
TEST_HOST = "$(BUNDLE_LOADER)";
372378
};

SwiftGif.xcodeproj/xcuserdata/arne.xcuserdatad/xcschemes/SwiftGif.xcscheme SwiftGif.xcodeproj/xcshareddata/xcschemes/SwiftGif.xcscheme

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0600"
3+
LastUpgradeVersion = "0700"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -23,10 +23,10 @@
2323
</BuildActionEntries>
2424
</BuildAction>
2525
<TestAction
26+
buildConfiguration = "Debug"
2627
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2728
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
28-
shouldUseLaunchSchemeArgsEnv = "YES"
29-
buildConfiguration = "Debug">
29+
shouldUseLaunchSchemeArgsEnv = "YES">
3030
<Testables>
3131
<TestableReference
3232
skipped = "NO">
@@ -48,17 +48,21 @@
4848
ReferencedContainer = "container:SwiftGif.xcodeproj">
4949
</BuildableReference>
5050
</MacroExpansion>
51+
<AdditionalOptions>
52+
</AdditionalOptions>
5153
</TestAction>
5254
<LaunchAction
55+
buildConfiguration = "Debug"
5356
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5457
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
5558
launchStyle = "0"
5659
useCustomWorkingDirectory = "NO"
57-
buildConfiguration = "Debug"
5860
ignoresPersistentStateOnLaunch = "NO"
5961
debugDocumentVersioning = "YES"
62+
debugServiceExtension = "internal"
6063
allowLocationSimulation = "YES">
61-
<BuildableProductRunnable>
64+
<BuildableProductRunnable
65+
runnableDebuggingMode = "0">
6266
<BuildableReference
6367
BuildableIdentifier = "primary"
6468
BlueprintIdentifier = "14A76437194EFBB800A74B1F"
@@ -71,12 +75,13 @@
7175
</AdditionalOptions>
7276
</LaunchAction>
7377
<ProfileAction
78+
buildConfiguration = "Release"
7479
shouldUseLaunchSchemeArgsEnv = "YES"
7580
savedToolIdentifier = ""
7681
useCustomWorkingDirectory = "NO"
77-
buildConfiguration = "Release"
7882
debugDocumentVersioning = "YES">
79-
<BuildableProductRunnable>
83+
<BuildableProductRunnable
84+
runnableDebuggingMode = "0">
8085
<BuildableReference
8186
BuildableIdentifier = "primary"
8287
BlueprintIdentifier = "14A76437194EFBB800A74B1F"

SwiftGif.xcodeproj/xcuserdata/arne.xcuserdatad/xcschemes/xcschememanagement.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<dict>
55
<key>SchemeUserState</key>
66
<dict>
7-
<key>SwiftGif.xcscheme</key>
7+
<key>SwiftGif.xcscheme_^#shared#^_</key>
88
<dict>
99
<key>orderHint</key>
1010
<integer>0</integer>

SwiftGifCommon/UIImage+Gif.swift

+45-33
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,30 @@ import UIKit
1010
import ImageIO
1111

1212
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+
}
1719
return UIImage.animatedImageWithSource(source)
1820
}
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 {
2235
var delay = 0.1
23-
36+
2437
// Get dictionaries
2538
let cfProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil)
2639
let gifProperties: CFDictionaryRef = unsafeBitCast(
@@ -37,17 +50,16 @@ extension UIImage {
3750
delayObject = unsafeBitCast(CFDictionaryGetValue(gifProperties,
3851
unsafeAddressOf(kCGImagePropertyGIFDelayTime)), AnyObject.self)
3952
}
40-
53+
4154
delay = delayObject as! Double
42-
55+
4356
if delay < 0.1 {
4457
delay = 0.1 // Make sure they're not too fast
4558
}
46-
47-
59+
4860
return delay
4961
}
50-
62+
5163
class func gcdForPair(var a: Int?, var _ b: Int?) -> Int {
5264
// Check if one of them is nil
5365
if b == nil || a == nil {
@@ -59,19 +71,19 @@ extension UIImage {
5971
return 0
6072
}
6173
}
62-
74+
6375
// Swap for modulo
6476
if a < b {
6577
let c = a
6678
a = b
6779
b = c
6880
}
69-
81+
7082
// Get greatest common divisor
7183
var rest: Int
7284
while true {
7385
rest = a! % b!
74-
86+
7587
if rest == 0 {
7688
return b! // Found it
7789
} else {
@@ -80,70 +92,70 @@ extension UIImage {
8092
}
8193
}
8294
}
83-
95+
8496
class func gcdForArray(array: Array<Int>) -> Int {
8597
if array.isEmpty {
8698
return 1
8799
}
88-
100+
89101
var gcd = array[0]
90-
102+
91103
for val in array {
92104
gcd = UIImage.gcdForPair(val, gcd)
93105
}
94-
106+
95107
return gcd
96108
}
97-
98-
public class func animatedImageWithSource(source: CGImageSource) -> UIImage? {
109+
110+
class func animatedImageWithSource(source: CGImageSource) -> UIImage? {
99111
let count = CGImageSourceGetCount(source)
100112
var images = [CGImageRef]()
101113
var delays = [Int]()
102-
114+
103115
// Fill arrays
104116
for i in 0..<count {
105117
// Add image
106118
if let image = CGImageSourceCreateImageAtIndex(source, i, nil) {
107119
images.append(image)
108120
}
109-
121+
110122
// At it's delay in cs
111123
let delaySeconds = UIImage.delayForImageAtIndex(Int(i),
112124
source: source)
113125
delays.append(Int(delaySeconds * 1000.0)) // Seconds to ms
114126
}
115-
127+
116128
// Calculate full duration
117129
let duration: Int = {
118130
var sum = 0
119-
131+
120132
for val: Int in delays {
121133
sum += val
122134
}
123-
135+
124136
return sum
125137
}()
126-
138+
127139
// Get frames
128140
let gcd = gcdForArray(delays)
129141
var frames = [UIImage]()
130-
142+
131143
var frame: UIImage
132144
var frameCount: Int
133145
for i in 0..<count {
134146
frame = UIImage(CGImage: images[Int(i)])
135147
frameCount = Int(delays[Int(i)] / gcd)
136-
148+
137149
for _ in 0..<frameCount {
138150
frames.append(frame)
139151
}
140152
}
141-
153+
142154
// Heyhey
143155
let animation = UIImage.animatedImageWithImages(frames,
144156
duration: Double(duration) / 1000.0)
145-
157+
146158
return animation
147159
}
148-
160+
149161
}

SwiftGifDemo/Info.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<key>CFBundleExecutable</key>
88
<string>${EXECUTABLE_NAME}</string>
99
<key>CFBundleIdentifier</key>
10-
<string>me.arne.${PRODUCT_NAME:rfc1034identifier}</string>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1111
<key>CFBundleInfoDictionaryVersion</key>
1212
<string>6.0</string>
1313
<key>CFBundleName</key>

SwiftGifDemo/RootViewController.swift

+7-10
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,19 @@ class RootViewController: UIViewController {
2222
override func viewDidLoad() {
2323
super.viewDidLoad()
2424

25-
var imageData = NSData(contentsOfURL: NSBundle.mainBundle()
26-
.URLForResource("jeremy", withExtension: "gif")!)
27-
let jeremy = UIImage.animatedImageWithData(imageData!)
28-
var imageView = UIImageView(image: jeremy)
25+
let jeremyGif = UIImage.gifWithName("jeremy")
26+
let imageView = UIImageView(image: jeremyGif)
2927
imageView.frame = CGRect(x: 0.0, y: 20.0, width: 350.0, height: 202.0)
3028

3129
view.addSubview(imageView)
3230

3331

34-
imageData = NSData(contentsOfURL: NSBundle.mainBundle()
35-
.URLForResource("adventure-time", withExtension: "gif")!)
36-
let advTime = UIImage.animatedImageWithData(imageData!)
37-
imageView = UIImageView(image: advTime)
38-
imageView.frame = CGRect(x: 0.0, y: 222.0, width: 350.0, height: 202.0)
32+
let imageData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("adventure-time", withExtension: "gif")!)
33+
let advTimeGif = UIImage.gifWithData(imageData!)
34+
let imageView2 = UIImageView(image: advTimeGif)
35+
imageView2.frame = CGRect(x: 0.0, y: 222.0, width: 350.0, height: 202.0)
3936

40-
view.addSubview(imageView)
37+
view.addSubview(imageView2)
4138
}
4239

4340
override func didReceiveMemoryWarning() {

SwiftGifTests/Info.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<key>CFBundleExecutable</key>
88
<string>${EXECUTABLE_NAME}</string>
99
<key>CFBundleIdentifier</key>
10-
<string>me.arne.${PRODUCT_NAME:rfc1034identifier}</string>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1111
<key>CFBundleInfoDictionaryVersion</key>
1212
<string>6.0</string>
1313
<key>CFBundleName</key>

0 commit comments

Comments
 (0)