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

Commit

Permalink
Merge branch '1.5.2-development'
Browse files Browse the repository at this point in the history
* 1.5.2-development:
  Format code, bump version
  add  gifWithURL
  support tvos
  • Loading branch information
bahlo committed Jan 28, 2016
2 parents eb4ffd2 + 9167921 commit 56d5687
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 30 deletions.
80 changes: 52 additions & 28 deletions SwiftGifCommon/UIImage+Gif.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,54 @@ import UIKit
import ImageIO

extension UIImage {

public class func gifWithData(data: NSData) -> UIImage? {
// Create source from data
guard let source = CGImageSourceCreateWithData(data, nil) else {
print("SwiftGif: Source for the image does not exist")
return nil
}

return UIImage.animatedImageWithSource(source)
}


public class func gifWithURL(gifUrl:String) -> UIImage? {
// Validate URL
guard let bundleURL:NSURL? = NSURL(string: gifUrl)
else {
print("SwiftGif: This image named \"\(gifUrl)\" does not exist")
return nil
}

// Validate data
guard let imageData = NSData(contentsOfURL: bundleURL!) else {
print("SwiftGif: Cannot turn image named \"\(gifUrl)\" into NSData")
return nil
}

return gifWithData(imageData)
}

public class func gifWithName(name: String) -> UIImage? {
guard let bundleURL = NSBundle.mainBundle().URLForResource(name, withExtension: "gif") else {
// Check for existance of gif
guard let bundleURL = NSBundle.mainBundle()
.URLForResource(name, withExtension: "gif") else {
print("SwiftGif: This image named \"\(name)\" does not exist")
return nil
}

// Validate data
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(
Expand All @@ -50,16 +74,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 {
Expand All @@ -71,19 +95,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 {
Expand All @@ -92,70 +116,70 @@ extension UIImage {
}
}
}

class func gcdForArray(array: Array<Int>) -> Int {
if array.isEmpty {
return 1
}

var gcd = array[0]

for val in array {
gcd = UIImage.gcdForPair(val, gcd)
}

return gcd
}

class func animatedImageWithSource(source: CGImageSource) -> UIImage? {
let count = CGImageSourceGetCount(source)
var images = [CGImageRef]()
var delays = [Int]()

// Fill arrays
for i in 0..<count {
// Add image
if let image = CGImageSourceCreateImageAtIndex(source, i, nil) {
images.append(image)
}

// At it's delay in cs
let delaySeconds = UIImage.delayForImageAtIndex(Int(i),
source: source)
delays.append(Int(delaySeconds * 1000.0)) // Seconds to ms
}

// Calculate full duration
let duration: Int = {
var sum = 0

for val: Int in delays {
sum += val
}

return sum
}()

// Get frames
let gcd = gcdForArray(delays)
var frames = [UIImage]()

var frame: UIImage
var frameCount: Int
for i in 0..<count {
frame = UIImage(CGImage: images[Int(i)])
frameCount = Int(delays[Int(i)] / gcd)

for _ in 0..<frameCount {
frames.append(frame)
}
}

// Heyhey
let animation = UIImage.animatedImageWithImages(frames,
duration: Double(duration) / 1000.0)

return animation
}

}
5 changes: 3 additions & 2 deletions SwiftGifOrigin.podspec
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
Pod::Spec.new do |s|
s.name = 'SwiftGifOrigin'
s.version = '1.5.1'
s.version = '1.5.2'
s.summary = 'A small UIImage extension with gif support'
s.homepage = 'https://github.com/bahlo/SwiftGif'
s.license = 'MIT'
s.author = { 'Arne Bahlo': '[email protected]' }

s.ios.deployment_target = '8.0'
s.tvos.deployment_target = '9.0'

s.source = {
git: 'https://github.com/bahlo/SwiftGif.git',
tag: 'v1.5.1'
tag: 'v1.5.2'
}

s.source_files = 'SwiftGifCommon/*.swift'
Expand Down

0 comments on commit 56d5687

Please sign in to comment.