From ce384ae22a662da965c4464628694425a20e8715 Mon Sep 17 00:00:00 2001 From: Reda Lemeden Date: Thu, 22 Oct 2015 20:49:31 +0200 Subject: [PATCH] Fix array index out of range bug Closes #25 --- Gifu.xcodeproj/project.pbxproj | 4 ++++ Source/Animator.swift | 26 ++++++++++++-------------- Source/ArrayExtension.swift | 5 +++++ 3 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 Source/ArrayExtension.swift diff --git a/Gifu.xcodeproj/project.pbxproj b/Gifu.xcodeproj/project.pbxproj index a1155d0..e36e138 100644 --- a/Gifu.xcodeproj/project.pbxproj +++ b/Gifu.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 004529921BD82209006493BF /* Runes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 004529911BD82209006493BF /* Runes.swift */; }; 005656ED1A6F14D6008A0ED1 /* Animator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 005656EC1A6F14D6008A0ED1 /* Animator.swift */; }; 005656EF1A6F1C26008A0ED1 /* AnimatableImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 005656EE1A6F1C26008A0ED1 /* AnimatableImageView.swift */; }; + 007E08441BD95E6200883D0C /* ArrayExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 007E08431BD95E6200883D0C /* ArrayExtension.swift */; }; 009BD1391BBC7F6500FC982B /* GifuTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 009BD1381BBC7F6500FC982B /* GifuTests.swift */; }; 009BD13B1BBC7F6500FC982B /* Gifu.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 00B8C73E1A364DA400C188E7 /* Gifu.framework */; }; 009BD1441BBC93C800FC982B /* CGSizeExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 009BD1431BBC93C800FC982B /* CGSizeExtension.swift */; }; @@ -38,6 +39,7 @@ 004529911BD82209006493BF /* Runes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Runes.swift; path = Carthage/Checkouts/Runes/Source/Runes.swift; sourceTree = SOURCE_ROOT; }; 005656EC1A6F14D6008A0ED1 /* Animator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Animator.swift; sourceTree = ""; }; 005656EE1A6F1C26008A0ED1 /* AnimatableImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnimatableImageView.swift; sourceTree = ""; }; + 007E08431BD95E6200883D0C /* ArrayExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrayExtension.swift; sourceTree = ""; }; 009BD1361BBC7F6500FC982B /* GifuTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GifuTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 009BD1381BBC7F6500FC982B /* GifuTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GifuTests.swift; sourceTree = ""; }; 009BD13A1BBC7F6500FC982B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -84,6 +86,7 @@ children = ( 009BD1431BBC93C800FC982B /* CGSizeExtension.swift */, EAF49C7E1A3A4DE000B395DF /* UIImageExtension.swift */, + 007E08431BD95E6200883D0C /* ArrayExtension.swift */, ); name = Extensions; sourceTree = ""; @@ -282,6 +285,7 @@ EAF49CB11A3B6EEB00B395DF /* AnimatedFrame.swift in Sources */, 00B8C75F1A364DCE00C188E7 /* ImageSourceHelpers.swift in Sources */, EAF49C811A3A4FAA00B395DF /* FunctionalHelpers.swift in Sources */, + 007E08441BD95E6200883D0C /* ArrayExtension.swift in Sources */, EAF49C7F1A3A4DE000B395DF /* UIImageExtension.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Source/Animator.swift b/Source/Animator.swift index 4708292..86398cd 100644 --- a/Source/Animator.swift +++ b/Source/Animator.swift @@ -91,21 +91,19 @@ class Animator { /// - returns: An optional image at a given frame. func updateCurrentFrame(duration: CFTimeInterval) -> Bool { timeSinceLastFrameChange += min(maxTimeStep, duration) - let frameDuration = animatedFrames[currentFrameIndex].duration + guard let frameDuration = animatedFrames[safe:currentFrameIndex]?.duration where + frameDuration <= timeSinceLastFrameChange else { return false } - if timeSinceLastFrameChange >= frameDuration { - timeSinceLastFrameChange -= frameDuration - let lastFrameIndex = currentFrameIndex - currentFrameIndex = ++currentFrameIndex % animatedFrames.count - - // Loads the next needed frame for progressive loading - if animatedFrames.count < frameCount { - animatedFrames[lastFrameIndex] = prepareFrame(currentPreloadIndex) - currentPreloadIndex = ++currentPreloadIndex % frameCount - } - return true + timeSinceLastFrameChange -= frameDuration + let lastFrameIndex = currentFrameIndex + currentFrameIndex = ++currentFrameIndex % animatedFrames.count + + // Loads the next needed frame for progressive loading + if animatedFrames.count < frameCount { + animatedFrames[lastFrameIndex] = prepareFrame(currentPreloadIndex) + currentPreloadIndex = ++currentPreloadIndex % frameCount } - - return false + + return true } } diff --git a/Source/ArrayExtension.swift b/Source/ArrayExtension.swift new file mode 100644 index 0000000..dbbc371 --- /dev/null +++ b/Source/ArrayExtension.swift @@ -0,0 +1,5 @@ +extension Array { + subscript(safe index: Int) -> Element? { + return indices ~= index ? self[index] : .None + } +}