[ios] Add boundsProvider to renderToImage (#3002)

This commit is contained in:
Mario Zechner 2026-02-27 12:59:30 +01:00
parent 6968ebd8e8
commit 0fa873e3d9
4 changed files with 13 additions and 3 deletions

View File

@ -22,3 +22,4 @@
- Add option to disable drawing when out of viewport (#18) - Add option to disable drawing when out of viewport (#18)
- Add docs for public Spine classes/methods (#19) - Add docs for public Spine classes/methods (#19)
- Fix various regressions (#20) - Fix various regressions (#20)
- Add `boundsProvider` parameter to `SkeletonDrawableWrapper.renderToImage(...)` to align image rendering bounds with `SpineView` (#3002)

View File

@ -58,7 +58,7 @@ struct DressUp: View {
SpineView( SpineView(
from: .drawable(drawable), from: .drawable(drawable),
controller: model.controller, controller: model.controller,
boundsProvider: SkinAndAnimationBounds(skins: ["full-skins/girl"]) boundsProvider: model.boundsProvider
) )
} else { } else {
Spacer() Spacer()
@ -76,6 +76,7 @@ struct DressUp: View {
final class DressUpModel: ObservableObject { final class DressUpModel: ObservableObject {
let thumbnailSize = CGSize(width: 200, height: 200) let thumbnailSize = CGSize(width: 200, height: 200)
let boundsProvider: BoundsProvider = SkinAndAnimationBounds(skins: ["full-skins/girl"])
@Published @Published
var controller: SpineController var controller: SpineController
@ -118,6 +119,7 @@ final class DressUpModel: ObservableObject {
try skin.name.flatMap { skinName in try skin.name.flatMap { skinName in
self.skinImages[skinName] = try drawable.renderToImage( self.skinImages[skinName] = try drawable.renderToImage(
size: self.thumbnailSize, size: self.thumbnailSize,
boundsProvider: self.boundsProvider,
backgroundColor: .white, backgroundColor: .white,
scaleFactor: UIScreen.main.scale scaleFactor: UIScreen.main.scale
) )

View File

@ -37,11 +37,18 @@ public extension SkeletonDrawableWrapper {
/// ///
/// Parameters: /// Parameters:
/// - size: The size of the `CGImage` that should be rendered. /// - size: The size of the `CGImage` that should be rendered.
/// - boundsProvider: The skeleton bounds provider used to compute fitting and positioning.
/// - backgroundColor: the background color of the image /// - backgroundColor: the background color of the image
/// - scaleFactor: The scale factor. Set this to `UIScreen.main.scale` if you want to show the image in a view /// - scaleFactor: The scale factor. Set this to `UIScreen.main.scale` if you want to show the image in a view
func renderToImage(size: CGSize, backgroundColor: UIColor, scaleFactor: CGFloat = 1) throws -> CGImage? { func renderToImage(
size: CGSize,
boundsProvider: BoundsProvider = SetupPoseBounds(),
backgroundColor: UIColor,
scaleFactor: CGFloat = 1
) throws -> CGImage? {
let spineView = SpineUIView( let spineView = SpineUIView(
controller: SpineController(disposeDrawableOnDeInit: false), // Doesn't own the drawable controller: SpineController(disposeDrawableOnDeInit: false), // Doesn't own the drawable
boundsProvider: boundsProvider,
backgroundColor: backgroundColor backgroundColor: backgroundColor
) )
spineView.frame = CGRect(origin: .zero, size: size) spineView.frame = CGRect(origin: .zero, size: size)

View File

@ -46,7 +46,7 @@ import UIKit
/// To update the ``AnimationState`` and apply it to the ``Skeleton`` call the ``AnimationStateWrapper/update`` function, providing it /// To update the ``AnimationState`` and apply it to the ``Skeleton`` call the ``AnimationStateWrapper/update`` function, providing it
/// a delta time in seconds to advance the animations. /// a delta time in seconds to advance the animations.
/// ///
/// To render the current pose of the ``Skeleton`` as a `CGImage`, use ``SkeletonDrawableWrapper/renderToImage(size:backgroundColor:scaleFactor:)``. /// To render the current pose of the ``Skeleton`` as a `CGImage`, use ``SkeletonDrawableWrapper/renderToImage(size:boundsProvider:backgroundColor:scaleFactor:)``.
/// ///
/// When the skeleton drawable is no longer needed, call the ``SkeletonDrawableWrapper/dispose()`` method to release its resources. If /// When the skeleton drawable is no longer needed, call the ``SkeletonDrawableWrapper/dispose()`` method to release its resources. If
/// the skeleton drawable was constructed from a shared ``Atlas`` and ``SkeletonData``, make sure to dispose the /// the skeleton drawable was constructed from a shared ``Atlas`` and ``SkeletonData``, make sure to dispose the