Skip to content

Commit

Permalink
Fix issue where DotLottieImageProvider didn't handle base64 images (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
calda committed Jan 8, 2024
1 parent 9c1dac7 commit 2562590
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 24 deletions.
20 changes: 20 additions & 0 deletions Sources/Private/Model/Assets/ImageAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
// Created by Brandon Withrow on 1/9/19.
//

import CoreGraphics
import Foundation

#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif

// MARK: - ImageAsset

public final class ImageAsset: Asset {
Expand Down Expand Up @@ -111,3 +118,16 @@ extension Data {
}

}

extension ImageAsset {
/// A `CGImage` loaded from this asset if represented using a Base 64 encoding
var base64Image: CGImage? {
guard let data = Data(imageAsset: self) else { return nil }

#if canImport(UIKit)
return UIImage(data: data)?.cgImage
#elseif canImport(AppKit)
return NSImage(data: data)?.lottie_CGImage
#endif
}
}
6 changes: 5 additions & 1 deletion Sources/Private/Model/DotLottie/DotLottieImageProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ class DotLottieImageProvider: AnimationImageProvider {
let filepath: URL

func imageForAsset(asset: ImageAsset) -> CGImage? {
images[asset.name]
if let base64Image = asset.base64Image {
return base64Image
}

return images[asset.name]
}

// MARK: Private
Expand Down
13 changes: 0 additions & 13 deletions Sources/Public/Animation/LottieAnimationLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1333,19 +1333,6 @@ public class LottieAnimationLayer: CALayer {

self.animationContext = animationContext

switch currentRenderingEngine {
case .mainThread:
// On the CALayer side, just play all animations immediately. The UIView side
// will handle queueing animations.
break

case .coreAnimation, nil:
// The Core Animation engine automatically batches animation setup to happen
// in `CALayer.display()`, which won't be called until the layer is on-screen,
// so we don't need to defer animation setup at this layer.
break
}

animationID = animationID + 1
_activeAnimationName = LottieAnimationLayer.animationName + String(animationID)

Expand Down
7 changes: 2 additions & 5 deletions Sources/Public/iOS/BundleImageProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ public class BundleImageProvider: AnimationImageProvider {
// MARK: Public

public func imageForAsset(asset: ImageAsset) -> CGImage? {
if
let data = Data(imageAsset: asset),
let image = UIImage(data: data)
{
return image.cgImage
if let base64Image = asset.base64Image {
return base64Image
}

let imagePath: String?
Expand Down
7 changes: 2 additions & 5 deletions Sources/Public/macOS/BundleImageProvider.macOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ public class BundleImageProvider: AnimationImageProvider {
// MARK: Public

public func imageForAsset(asset: ImageAsset) -> CGImage? {
if
let data = Data(imageAsset: asset),
let image = NSImage(data: data)
{
return image.lottie_CGImage
if let base64Image = asset.base64Image {
return base64Image
}

let imagePath: String?
Expand Down
Binary file added Tests/Samples/Issues/issue_2265.lottie
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Supports Core Animation engine
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2562590

Please sign in to comment.