         }
         
         let frameProperties = imageProperties.map(){
-                unsafeBitCast(
-                    CFDictionaryGetValue($0,
-                        unsafeAddressOf(kCGImagePropertyGIFDictionary)),                CFDictionary.self)
+            unsafeBitCast(
+                CFDictionaryGetValue($0,
+                    unsafeAddressOf(kCGImagePropertyGIFDictionary)),                CFDictionary.self)
         }
         
         let frameDelays:[Float] = frameProperties.map(){
             var delayObject: AnyObject = unsafeBitCast(
-                    CFDictionaryGetValue($0,
-                        unsafeAddressOf(kCGImagePropertyGIFUnclampedDelayTime)),
-                    AnyObject.self)
+                CFDictionaryGetValue($0,
+                    unsafeAddressOf(kCGImagePropertyGIFUnclampedDelayTime)),
+                AnyObject.self)
+            
             let EPS = 10e-10
             if(delayObject.doubleValue<EPS){
                 delayObject = unsafeBitCast(CFDictionaryGetValue($0,
-                        unsafeAddressOf(kCGImagePropertyGIFDelayTime)), AnyObject.self)
+                    unsafeAddressOf(kCGImagePropertyGIFDelayTime)), AnyObject.self)
             }
             return delayObject as! Float
         }
         return frameDelays
     }
     
-    typealias FrameDelays = [Float]
-    private func CalculateFrameDelay(var delays:FrameDelays)
+    private func CalculateFrameDelay(var delays:[Float])
     {
         
         //Factors send to CADisplayLink.frameInterval

public class JWAnimatedImageView:UIImageView
{
    
    
    
    //bound to function 'updateFrame'
    private lazy var displayLink: CADisplayLink = CADisplayLink(target: self, selector: Selector("updateFrame"))
    
    // obtained from NSData
    private var imageSource: CGImageSourceRef?
    
    //as a frame cache queue,obtained by 'frameProducter',max size is 'frameCacheMaxNumber'
    private var frameCache = [UIImage]()
    
    //frameCacheMaxNumber's default is 5
    private var frameCacheMaxNumber = 5
    public func setFrameCacheMaxNumber(newFrameCacheMaxNumber:Int)
        {self.frameCacheMaxNumber = newFrameCacheMaxNumber}
    
    //indexs of frames choosed from imageSource and total number
    private var displayOrder = [Int]()
    private var imageCount = 0  //initial
    
    
    //the level of integrity of a gif image,The range is 0%(0)~100%(1).we know that CADisplayLink.frameInterval affact the display frames per second,if it is larger, we will only dispaly fewer frames per second,in the other way,we will never display some of frames all the time.So,the level of integrity gives us a limit that the device should show how many frames at list.If it is 100%(1),that means the device displays frames as much as it can.The default number is 0.8,but you can decrease it for a less cpu usage.
    private var levelOfIntegrity:Float = 0.8    //default
    private var displayRefreshFactors = 1   //default
    public func setLevelOfIntegrity(newLevelOfIntegrity:Float)
        {self.levelOfIntegrity = newLevelOfIntegrity}
    
    
    
    //add gif as NSData
    public func addGifImage(data:NSData){
        self.imageSource = CGImageSourceCreateWithData(data, nil)
        
        //set cover
        image = UIImage(CGImage: CGImageSourceCreateImageAtIndex(self.imageSource!,0,nil)!)//cover
        //self.layer.setNeedsDisplay()

        CalculateFrameDelay(GetDelayTimes())
        setCurrentImage()
        
        self.displayLink.frameInterval = self.displayRefreshFactors
        self.displayLink.addToRunLoop(.mainRunLoop(), forMode: NSRunLoopCommonModes)
    }
    
    public var currentImage:UIImage?
    
    //the current index of frames when display the gif
    private var displayOrderIndex = 0
    
    //this is an empty function,but program won't work if I delete it,and I don't know why
    override public func displayLayer(layer: CALayer){}

            var framelosecount = 0
            for j in 1..<displayPosition.count{
                if(displayPosition[j] == displayPosition[j-1])
                {++framelosecount}
            }
            
            if(Float(framelosecount) <= Float(displayPosition.count) * (1.0 - self.levelOfIntegrity)||i==displayRefreshDelayTime.count-1){
                
                self.imageCount = displayPosition.last!
                self.displayRefreshFactors = displayRefreshFactors[i]
                
                var indexOfold = 0
                var indexOfnew = 0
                
