崩溃信息如下:
这是我的代码,如下:
class TTSpinePetView: UIView {
var spineView: SpineUIView?
var controller: SpineController?
var customSkin: Skin?
var drawable: SkeletonDrawableWrapper?
var allAnimations: [String] = []
var allSkinsName: [String] = []
var spineSize: CGSize?
var petModel: TTPetModel?
var isInitialized: Bool = false
convenience init() {
self.init(frame: .zero)
}
func setupView(model: TTPetModel?, size: CGSize?) {
guard let model = model else {
return
}
guard let size = size else {
return
}
if (model.finalSlots.count ?? 0) < 1 {
return
}
if String.isEmpty(model.animation) {
return
}
self.petModel = model
self.spineSize = size
// 初始化 Spine 控制器
controller = SpineController(
onInitialized: {[weak self] controller in
guard let self = self else {
return
}
if self.allAnimations.contains(model.animation) {
controller.animationState.setAnimationByName(
trackIndex: 0,
animationName: model.animation,
loop: true
)
}
self.isInitialized = true
},
disposeDrawableOnDeInit: false
)
let loadDrawable: () async throws -> SkeletonDrawableWrapper = {
if let atlasUrl = self.findSpinePath(fileName: "cat.atlas"), let skelUrl = self.findSpinePath(fileName: "cat.skel") {
return try await SkeletonDrawableWrapper.fromFile(atlasFile: atlasUrl, skeletonFile: skelUrl)
} else {
let bundle = Bundle.load(with: TTSpinePetView.self, bundleName: "TTUI")
return try await SkeletonDrawableWrapper.fromBundle(atlasFileName: "cat.atlas", skeletonFileName: "cat.skel", bundle: bundle ?? Bundle.main)
}
}
guard let controller = self.controller else {
return
}
Task.detached(priority: .high) {
do {
let drawable = try await loadDrawable()
await MainActor.run {
self.drawable = drawable
let skeleton = drawable.skeleton
self.allAnimations = drawable.skeletonData.animations.compactMap { $0.name }
ttprint("allAnimations===> \(self.allAnimations)")
self.allSkinsName = drawable.skeletonData.skins.compactMap { $0.name }
ttprint("allSkinsName===> \(self.allSkinsName)")
let spineView = SpineUIView(from: .drawable(drawable),
controller: controller,
mode: .fit,
alignment: .center,
boundsProvider: SetupPoseBounds(),
backgroundColor: .clear)
self.addSubview(spineView)
spineView.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
self.applyCostumes()
self.spineView = spineView
}
} catch {
print("Failed to load skeleton: \(error)")
}
}
}
private func applyCostumes() {
guard let drawable = self.drawable else {
print("Drawable is not initialized")
return
}
customSkin?.dispose()
let skeleton = drawable.skeleton
customSkin = Skin.create(name: "custom-skin")
let costumeNames = petModel?.finalSlots ?? []
ttprint("costumeNames===> \(costumeNames)")
for costumeName in costumeNames {
if self.allSkinsName.contains(costumeName), let skin = drawable.skeletonData.findSkin(name: costumeName) {
customSkin?.addSkin(other: skin)
} else {
ttlog.info(event: "⚠️⚠️⚠️未找到该skin", param: ["skin": costumeName, "allSkinsName": allSkinsName.joined(separator: ",")], isAliLog: true)
}
}
skeleton.skin = customSkin
skeleton.setToSetupPose()
skeleton.updateWorldTransform(physics: SPINE_PHYSICS_UPDATE)
}
func findSpinePath(fileName: String) -> URL? {
var atlasUrl = TTAvatarResourceManager.rootPath(giftId: "cat") + "/\(fileName)"
if FileManager.default.fileExists(atPath: atlasUrl) {
return URL(string: "file://\(atlasUrl)")
}
return nil
}
deinit {
print("drawable?.dispose()释放了")
drawable?.dispose()
customSkin?.dispose()
}
}
如果我把 applyCostumes()
放到 onInitialized
后,那么我的视图变得非常大
我应该如何修改???