Add needsPrescaling public property

- This exposes the ability to turn off frame prescaling.
- Closes #40
This commit is contained in:
Storix 2016-03-13 13:30:27 +02:00 committed by Reda Lemeden
parent 0b77356888
commit ad67756cef
2 changed files with 15 additions and 4 deletions

View File

@ -10,6 +10,9 @@ public class AnimatableImageView: UIImageView {
/// The size of the frame cache.
public var framePreloadCount = 50
/// Specifies whether the GIF frames should be pre-scaled to save memory. Default is **true**.
public var needsPrescaling = true
/// A computed property that returns whether the image view is animating.
public var isAnimatingGIF: Bool {
return !displayLink.paused
@ -35,6 +38,7 @@ public class AnimatableImageView: UIImageView {
public func prepareForAnimation(imageData data: NSData) {
image = UIImage(data: data)
animator = Animator(data: data, size: frame.size, contentMode: contentMode, framePreloadCount: framePreloadCount)
animator?.needsPrescaling = needsPrescaling
animator?.prepareFrames()
attachDisplayLink()
}

View File

@ -23,6 +23,9 @@ class Animator {
var currentPreloadIndex = 0
/// Time elapsed since the last frame change. Used to determine when the frame should be updated.
var timeSinceLastFrameChange: NSTimeInterval = 0.0
/// Specifies whether GIF frames should be pre-scaled.
/// - seealso: `needsPrescaling` in AnimatableImageView.
var needsPrescaling = true
/// The current image frame to show.
var currentFrame: UIImage? {
@ -69,11 +72,15 @@ class Animator {
let image = UIImage(CGImage: frameImageRef)
let scaledImage: UIImage?
if needsPrescaling == true {
switch contentMode {
case .ScaleAspectFit: scaledImage = image.resizeAspectFit(size)
case .ScaleAspectFill: scaledImage = image.resizeAspectFill(size)
default: scaledImage = image.resize(size)
}
} else {
scaledImage = image
}
return AnimatedFrame(image: scaledImage, duration: frameDuration)
}