From 2323145e8321d0dcfc40d1f6c45c6390ec0deb0a Mon Sep 17 00:00:00 2001 From: Francisco Couceiro Date: Mon, 14 Feb 2022 12:34:33 +0000 Subject: [PATCH] Decouple JellyGifOperation from Foundation.Operation --- JellyGif/Source/JellyGifAnimator.swift | 6 +++--- JellyGif/Source/JellyGifOperation.swift | 13 +++++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/JellyGif/Source/JellyGifAnimator.swift b/JellyGif/Source/JellyGifAnimator.swift index 8cd9130..7e7748a 100644 --- a/JellyGif/Source/JellyGifAnimator.swift +++ b/JellyGif/Source/JellyGifAnimator.swift @@ -9,7 +9,7 @@ import UIKit ///Methods for managing JellyGifAnimator -public protocol JellyGifAnimatorDelegate: class { +public protocol JellyGifAnimatorDelegate: AnyObject { func gifAnimatorIsReady(_ sender: JellyGifAnimator) ///Registers an UIImageView to display GIF frames @@ -113,8 +113,8 @@ public class JellyGifAnimator { self?.preparingOperation = nil } - JellyGifAnimator.gifQueue.async { - self.preparingOperation?.start() + JellyGifAnimator.gifQueue.async { [weak self] in + self?.preparingOperation?.start() } } diff --git a/JellyGif/Source/JellyGifOperation.swift b/JellyGif/Source/JellyGifOperation.swift index 419cf83..459d529 100644 --- a/JellyGif/Source/JellyGifOperation.swift +++ b/JellyGif/Source/JellyGifOperation.swift @@ -9,21 +9,22 @@ import UIKit ///An operation object used to prepare information needed to start GIF animation -public class JellyGifOperation: Operation { +public class JellyGifOperation { public let inputInfo: GifInfo public let pixelSize: GifPixelSize public var completionHandler: ([UIImage], [CFTimeInterval]) -> Void + private var isCancelled = false + public init(info: GifInfo, pixelSize: GifPixelSize, completion: @escaping ([UIImage], [CFTimeInterval]) -> Void) { self.inputInfo = info self.pixelSize = pixelSize self.completionHandler = completion - super.init() } - override public func main() { - guard isCancelled == false else { return } + public func start() { + guard self.isCancelled == false else { return } let imageSource = CGImageSource.sourceFromInfo(inputInfo) let frames = imageSource?.frameDurations ?? [] @@ -41,4 +42,8 @@ public class JellyGifOperation: Operation { self?.completionHandler(images, frames) } } + + public func cancel() { + self.isCancelled = true + } }