Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Now it's time to move to Lottie 3.4, which solve the performance issue #7

Merged
merged 6 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "SDWebImage/SDWebImage" ~> 5.10
github "airbnb/lottie-ios" ~> 2.5
github "airbnb/lottie-ios" ~> 3.4
4 changes: 2 additions & 2 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "SDWebImage/SDWebImage" "5.5.2"
github "airbnb/lottie-ios" "2.5.3"
github "SDWebImage/SDWebImage" "5.13.0"
github "airbnb/lottie-ios" "3.4.0"
16 changes: 9 additions & 7 deletions Example/SDWebImageLottiePlugin Example macOS/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//
// AppDelegate.swift
// SDWebImageLottiePlugin_Example macOS
//
// Created by 李卓立 on 2020/2/29.
// Copyright © 2020 CocoaPods. All rights reserved.
//
/*
* This file is part of the SDWebImage package.
* (c) DreamPiggy <lizhuoli1126@126.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import Cocoa
import Lottie

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
Expand All @@ -15,6 +16,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
LottieConfiguration.shared.renderingEngine = .coreAnimation
}

func applicationWillTerminate(_ aNotification: Notification) {
Expand Down
37 changes: 18 additions & 19 deletions Example/SDWebImageLottiePlugin Example macOS/ViewController.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
//
// ViewController.swift
// SDWebImageLottiePlugin_Example macOS
//
// Created by 李卓立 on 2020/2/29.
// Copyright © 2020 CocoaPods. All rights reserved.
//
/*
* This file is part of the SDWebImage package.
* (c) DreamPiggy <lizhuoli1126@126.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import Cocoa
import Lottie
import SDWebImageLottiePlugin
import SDWebImage

class ViewController: NSViewController {

let animationView = LOTAnimationView()
let animationView = AnimationView()

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -22,17 +23,15 @@ class ViewController: NSViewController {
view.addSubview(animationView)

let lottieUrl = URL(string: "https://raw.githubusercontent.com/airbnb/lottie-web/master/demo/adrock/data.json")
animationView.sd_setImage(with: lottieUrl) { (image, error, cacheType, url) in
self.animationView.play(fromProgress: 0,
toProgress: 1,
withCompletion: { (finished) in
if finished {
print("Animation Complete")
} else {
print("Animation cancelled")
}
})
}
animationView.sd_setImage(with: lottieUrl, completed: { _,_,_,_ in
self.animationView.play(fromProgress: 0, toProgress: 1, loopMode: .repeat(5)) { finished in
if finished {
print("Animation Complete")
} else {
print("Animation cancelled")
}
}
})
}

}
Expand Down
2 changes: 2 additions & 0 deletions Example/SDWebImageLottiePlugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@
"$(inherited)",
);
INFOPLIST_FILE = Tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -781,6 +782,7 @@
"$(inherited)",
);
INFOPLIST_FILE = Tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
2 changes: 2 additions & 0 deletions Example/SDWebImageLottiePlugin/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import UIKit
import Lottie

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Expand All @@ -15,6 +16,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
LottieConfiguration.shared.renderingEngine = .coreAnimation
// Override point for customization after application launch.
return true
}
Expand Down
27 changes: 13 additions & 14 deletions Example/SDWebImageLottiePlugin/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import UIKit
import Lottie
import SDWebImageLottiePlugin
import SDWebImage

class ViewController: UIViewController {

let animationView = LOTAnimationView()

let animationView = AnimationView()
override func viewDidLoad() {
super.viewDidLoad()
animationView.contentMode = .scaleAspectFit
Expand All @@ -22,18 +23,16 @@ class ViewController: UIViewController {
view.addSubview(animationView)

let lottieUrl = URL(string: "https://raw.githubusercontent.com/airbnb/lottie-web/master/demo/gatin/data.json")
animationView.sd_setImage(with: lottieUrl) { (image, error, cacheType, url) in
self.animationView.play(fromProgress: 0,
toProgress: 1,
withCompletion: { (finished) in
if finished {
print("Animation Complete")
} else {
print("Animation cancelled")
}
})
}
animationView.sd_setImage(with: lottieUrl, completed: { _,_,_,_ in
self.animationView.play(fromProgress: 0, toProgress: 1, loopMode: .repeat(5)) { finished in
if finished {
print("Animation Complete")
} else {
print("Animation cancelled")
}
}
})
}

}

64 changes: 48 additions & 16 deletions Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,76 @@ class Tests: XCTestCase {

func testAnimatedImageViewLoad() {
let exception = self.expectation(description: "AnimationView load lottie URL")
let animationView = LOTAnimationView()
let animationView = AnimationView()
let lottieURL = URL(string: "https://raw.githubusercontent.com/airbnb/lottie-web/master/demo/happy2016/data.json")
animationView.sd_setImage(with: lottieURL) { (image, error, cacheType, url) in
animationView.sd_setImage(with: lottieURL, completed: { (image, error, cacheType, url) in
XCTAssertNil(error)
let lottieImage = try! XCTUnwrap(image)
XCTAssertTrue(lottieImage.isKind(of: LOTAnimatedImage.self))
let animation = try! XCTUnwrap((lottieImage as! LOTAnimatedImage).composition)
XCTAssertEqual(animation.compBounds.size, CGSize(width: 1920, height: 1080))
XCTAssertTrue(lottieImage.isKind(of: LottieImage.self))
let animation = try! XCTUnwrap((lottieImage as! LottieImage).animation)
XCTAssertEqual(animation.size, CGSize(width: 1920, height: 1080))
exception.fulfill()
}
})
self.waitForExpectations(timeout: 5, handler: nil)
}

func testAnimatedControlLoad() {
let exception = self.expectation(description: "AnimatedControl load lottie URL")
let animationView = LOTAnimatedSwitch()
let animationView = AnimatedSwitch()
let lottieURL = URL(string: "https://raw.githubusercontent.com/airbnb/lottie-web/master/demo/adrock/data.json")
animationView.sd_setImage(with: lottieURL) { (image, error, cacheType, url) in
animationView.sd_setImage(with: lottieURL, completed: { (image, error, cacheType, url) in
XCTAssertNil(error)
let lottieImage = try! XCTUnwrap(image)
XCTAssertTrue(lottieImage.isKind(of: LOTAnimatedImage.self))
let animation = try! XCTUnwrap((lottieImage as! LOTAnimatedImage).composition)
XCTAssertEqual(animation.compBounds.size, CGSize(width: 690, height: 913))
XCTAssertTrue(lottieImage.isKind(of: LottieImage.self))
let animation = try! XCTUnwrap((lottieImage as! LottieImage).animation)
XCTAssertEqual(animation.size, CGSize(width: 690, height: 913))
exception.fulfill()
}
})
self.waitForExpectations(timeout: 5, handler: nil)
}

func testLottieImageWithBundle() throws {
let bundle = Bundle(for: type(of: self))
let fileURL = bundle.url(forResource: "Assets", withExtension: "json")!
let lottieData = try Data(contentsOf: fileURL)
let context = [SDWebImageContextOption.lottieBundle : bundle]
let lottieImage = LOTAnimatedImage(data: lottieData, scale: 1, options: [.webImageContext: context])!
let animation = try JSONDecoder().decode(Animation.self, from: lottieData)
let animationView = AnimationView(animation: animation, imageProvider: BundleImageProvider(bundle: bundle, searchPath: nil))
let renderer = SDGraphicsImageRenderer(size: animation.size)
let viewImage = renderer.image { context in
animationView.drawHierarchy(in: CGRect(origin: CGPoint(x: 0, y: 0), size: animation.size), afterScreenUpdates: true)
}
// Pick the color to check
let color1 = try XCTUnwrap(viewImage.sd_color(at: CGPoint(x: 150, y: 150)))
XCTAssertEqual(color1.toHexString(), "#00d1c1");

let lottieImage = LottieImage(animation: animation)
lottieImage.imageProvider = BundleImageProvider(bundle: bundle, searchPath: nil)
let posterFrame = try XCTUnwrap(lottieImage.animatedImageFrame(at: 0))
// Pick the color to check
let color = try XCTUnwrap(posterFrame.sd_color(at: CGPoint(x: 150, y: 150)))
XCTAssertEqual(color.toHexString(), "#00d1c1");
let color2 = try XCTUnwrap(posterFrame.sd_color(at: CGPoint(x: 150, y: 150)))
XCTAssertEqual(color2.toHexString(), "#00d1c1");
}


func testLottieImageExtractFrame() {
let exception = self.expectation(description: "LottieImage extract frame")
let lottieUrl = URL(string: "https://raw.githubusercontent.com/airbnb/lottie-web/master/demo/gatin/data.json")!
let task = URLSession.shared.dataTask(with: lottieUrl) { data, _, _ in
if let data = data, let animation = try? JSONDecoder().decode(Animation.self, from: data) {
let lottieImage = LottieImage(animation: animation)
let frameCount = lottieImage.animatedImageFrameCount
XCTAssertEqual(frameCount, 80)
let posterFrame = lottieImage.animatedImageFrame(at: 0)
let lastFrame = lottieImage.animatedImageFrame(at: frameCount - 1)
XCTAssertNotNil(posterFrame)
XCTAssertNotNil(lastFrame)
XCTAssertNotEqual(posterFrame, lastFrame)
exception.fulfill()
} else {
XCTFail()
}
}
task.resume()
self.waitForExpectations(timeout: 5, handler: nil)
}
}
25 changes: 25 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"object": {
"pins": [
{
"package": "Lottie",
"repositoryURL": "https://github.com/airbnb/lottie-ios.git",
"state": {
"branch": null,
"revision": "246bab7ef72bad56abefb88e84a08871cecf9cb8",
"version": "3.4.0"
}
},
{
"package": "SDWebImage",
"repositoryURL": "https://github.com/SDWebImage/SDWebImage.git",
"state": {
"branch": null,
"revision": "c4b8660bb3ef543fe4bdcaac0db956b32dc5583f",
"version": "5.13.0"
}
}
]
},
"version": 1
}
36 changes: 36 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SDWebImageLottiePlugin",
platforms: [
.iOS(.v11),
.tvOS(.v11),
.macOS(.v10_11)
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "SDWebImageLottiePlugin",
targets: ["SDWebImageLottiePlugin"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/SDWebImage/SDWebImage.git", from: "5.10.0"),
.package(url: "https://github.com/airbnb/lottie-ios.git", from: "3.4.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "SDWebImageLottiePlugin",
dependencies: ["SDWebImage", "Lottie"],
path: ".",
sources: ["SDWebImageLottiePlugin/Classes"],
publicHeadersPath: "SDWebImageLottiePlugin/Classes"
)
]
)
Loading