diff --git a/.travis.yml b/.travis.yml index 49b7066..fffd125 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,2 +1,8 @@ -language: swift +language: objective-c +osx_image: xcode7 +script: + - xctool + -project SwiftGif.xcodeproj -scheme SwiftGif + -sdk iphonesimulator build test + CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY="" diff --git a/README.md b/README.md index 956edf6..d41f46a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# SwiftGif +# 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) A small `UIImage` extension with gif support. @@ -7,12 +7,11 @@ A small `UIImage` extension with gif support. ## Usage Import the `Gif.swift` in your project and do the following: ```swift -// jeremy.gif -var url = NSBundle.mainBundle().URLForResource("jeremy", withExtension: "gif") -var imageData = NSData(contentsOfURL: url) - // Returns an animated UIImage -UIImage.animatedImageWithData(imageData) +let jeremyGif = UIImage.gifWithName("jeremy") + +// Use the UIImage in your UIImageView +let imageView = UIImageView(image: jeremyGif) ``` ## How does it work? diff --git a/SwiftGif.xcodeproj/project.pbxproj b/SwiftGif.xcodeproj/project.pbxproj index a1059ef..ff88991 100644 --- a/SwiftGif.xcodeproj/project.pbxproj +++ b/SwiftGif.xcodeproj/project.pbxproj @@ -155,7 +155,8 @@ 14A76430194EFBB800A74B1F /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0600; + LastSwiftUpdateCheck = 0700; + LastUpgradeCheck = 0700; ORGANIZATIONNAME = "Arne Bahlo"; TargetAttributes = { 14A76437194EFBB800A74B1F = { @@ -257,6 +258,7 @@ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; GCC_OPTIMIZATION_LEVEL = 0; @@ -321,6 +323,7 @@ ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; INFOPLIST_FILE = "$(SRCROOT)/SwiftGifDemo/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "me.arne.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Debug; @@ -332,6 +335,7 @@ ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; INFOPLIST_FILE = "$(SRCROOT)/SwiftGifDemo/Info.plist"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = "me.arne.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Release; @@ -351,6 +355,7 @@ INFOPLIST_FILE = SwiftGifTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; METAL_ENABLE_DEBUG_INFO = YES; + PRODUCT_BUNDLE_IDENTIFIER = "me.arne.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = "$(TARGET_NAME)"; TEST_HOST = "$(BUNDLE_LOADER)"; }; @@ -367,6 +372,7 @@ INFOPLIST_FILE = SwiftGifTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; METAL_ENABLE_DEBUG_INFO = NO; + PRODUCT_BUNDLE_IDENTIFIER = "me.arne.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_NAME = "$(TARGET_NAME)"; TEST_HOST = "$(BUNDLE_LOADER)"; }; diff --git a/SwiftGif.xcodeproj/xcuserdata/arne.xcuserdatad/xcschemes/SwiftGif.xcscheme b/SwiftGif.xcodeproj/xcshareddata/xcschemes/SwiftGif.xcscheme similarity index 90% rename from SwiftGif.xcodeproj/xcuserdata/arne.xcuserdatad/xcschemes/SwiftGif.xcscheme rename to SwiftGif.xcodeproj/xcshareddata/xcschemes/SwiftGif.xcscheme index d58a5ea..db5b63b 100644 --- a/SwiftGif.xcodeproj/xcuserdata/arne.xcuserdatad/xcschemes/SwiftGif.xcscheme +++ b/SwiftGif.xcodeproj/xcshareddata/xcschemes/SwiftGif.xcscheme @@ -1,6 +1,6 @@ + shouldUseLaunchSchemeArgsEnv = "YES"> @@ -48,17 +48,21 @@ ReferencedContainer = "container:SwiftGif.xcodeproj"> + + - + - + SchemeUserState - SwiftGif.xcscheme + SwiftGif.xcscheme_^#shared#^_ orderHint 0 diff --git a/SwiftGifCommon/UIImage+Gif.swift b/SwiftGifCommon/UIImage+Gif.swift index 1f6f078..b217ba5 100755 --- a/SwiftGifCommon/UIImage+Gif.swift +++ b/SwiftGifCommon/UIImage+Gif.swift @@ -10,17 +10,30 @@ import UIKit import ImageIO extension UIImage { - - public class func animatedImageWithData(data: NSData) -> UIImage? { - guard let source = CGImageSourceCreateWithData(data, nil) else { return nil } - + + public class func gifWithData(data: NSData) -> UIImage? { + guard let source = CGImageSourceCreateWithData(data, nil) else { + print("SwiftGif: Source for the image does not exist") + return nil + } return UIImage.animatedImageWithSource(source) } - - class func delayForImageAtIndex(index: Int, source: CGImageSource!) - -> Double { + + public class func gifWithName(name: String) -> UIImage? { + guard let bundleURL = NSBundle.mainBundle().URLForResource(name, withExtension: "gif") else { + print("SwiftGif: This image named \"\(name)\" does not exist") + return nil + } + guard let imageData = NSData(contentsOfURL: bundleURL) else { + print("SwiftGif: Cannot turn image named \"\(name)\" into NSData") + return nil + } + return gifWithData(imageData) + } + + class func delayForImageAtIndex(index: Int, source: CGImageSource!) -> Double { var delay = 0.1 - + // Get dictionaries let cfProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil) let gifProperties: CFDictionaryRef = unsafeBitCast( @@ -37,17 +50,16 @@ extension UIImage { delayObject = unsafeBitCast(CFDictionaryGetValue(gifProperties, unsafeAddressOf(kCGImagePropertyGIFDelayTime)), AnyObject.self) } - + delay = delayObject as! Double - + if delay < 0.1 { delay = 0.1 // Make sure they're not too fast } - - + return delay } - + class func gcdForPair(var a: Int?, var _ b: Int?) -> Int { // Check if one of them is nil if b == nil || a == nil { @@ -59,19 +71,19 @@ extension UIImage { return 0 } } - + // Swap for modulo if a < b { let c = a a = b b = c } - + // Get greatest common divisor var rest: Int while true { rest = a! % b! - + if rest == 0 { return b! // Found it } else { @@ -80,70 +92,70 @@ extension UIImage { } } } - + class func gcdForArray(array: Array) -> Int { if array.isEmpty { return 1 } - + var gcd = array[0] - + for val in array { gcd = UIImage.gcdForPair(val, gcd) } - + return gcd } - - public class func animatedImageWithSource(source: CGImageSource) -> UIImage? { + + class func animatedImageWithSource(source: CGImageSource) -> UIImage? { let count = CGImageSourceGetCount(source) var images = [CGImageRef]() var delays = [Int]() - + // Fill arrays for i in 0..CFBundleExecutable ${EXECUTABLE_NAME} CFBundleIdentifier - me.arne.${PRODUCT_NAME:rfc1034identifier} + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/SwiftGifDemo/RootViewController.swift b/SwiftGifDemo/RootViewController.swift index 0e21fb7..82f596b 100644 --- a/SwiftGifDemo/RootViewController.swift +++ b/SwiftGifDemo/RootViewController.swift @@ -22,22 +22,19 @@ class RootViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - var imageData = NSData(contentsOfURL: NSBundle.mainBundle() - .URLForResource("jeremy", withExtension: "gif")!) - let jeremy = UIImage.animatedImageWithData(imageData!) - var imageView = UIImageView(image: jeremy) + let jeremyGif = UIImage.gifWithName("jeremy") + let imageView = UIImageView(image: jeremyGif) imageView.frame = CGRect(x: 0.0, y: 20.0, width: 350.0, height: 202.0) view.addSubview(imageView) - imageData = NSData(contentsOfURL: NSBundle.mainBundle() - .URLForResource("adventure-time", withExtension: "gif")!) - let advTime = UIImage.animatedImageWithData(imageData!) - imageView = UIImageView(image: advTime) - imageView.frame = CGRect(x: 0.0, y: 222.0, width: 350.0, height: 202.0) + let imageData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("adventure-time", withExtension: "gif")!) + let advTimeGif = UIImage.gifWithData(imageData!) + let imageView2 = UIImageView(image: advTimeGif) + imageView2.frame = CGRect(x: 0.0, y: 222.0, width: 350.0, height: 202.0) - view.addSubview(imageView) + view.addSubview(imageView2) } override func didReceiveMemoryWarning() { diff --git a/SwiftGifTests/Info.plist b/SwiftGifTests/Info.plist index 2d1f328..6d32c15 100644 --- a/SwiftGifTests/Info.plist +++ b/SwiftGifTests/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable ${EXECUTABLE_NAME} CFBundleIdentifier - me.arne.${PRODUCT_NAME:rfc1034identifier} + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName