fix
杨锴
2025-05-06 fdb1d18a0b4b941b986d55f66c589e29836494eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
//  LaunchImageHelper.swift
//  WanPai
//
//  Created by 无故事王国 on 2023/10/19.
//
 
import Foundation
 
class LaunchImageHelper {
    static func snapshotStoryboard(sbName: String, isPortrait: Bool) -> UIImage? {
        if sbName.isEmpty {
            return nil
        }
 
        let storyboard = UIStoryboard(name: sbName, bundle: nil)
        guard let vc = storyboard.instantiateInitialViewController() else {
            return nil
        }
 
        vc.view.frame = UIScreen.main.bounds
        if isPortrait {
            if vc.view.frame.size.width > vc.view.frame.size.height {
                vc.view.frame = CGRect(x: 0, y: 0, width: vc.view.frame.size.height, height: vc.view.frame.size.width)
            }
        } else {
            if vc.view.frame.size.width < vc.view.frame.size.height {
                vc.view.frame = CGRect(x: 0, y: 0, width: vc.view.frame.size.height, height: vc.view.frame.size.width)
            }
        }
 
        vc.view.setNeedsLayout()
        vc.view.layoutIfNeeded()
 
        UIGraphicsBeginImageContextWithOptions(vc.view.frame.size, false, UIScreen.main.scale)
        vc.view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
 
    static func snapshotStoryboardForPortrait(sbName: String) -> UIImage? {
        return snapshotStoryboard(sbName: sbName, isPortrait: true)
    }
 
    static func snapshotStoryboardForLandscape(sbName: String) -> UIImage? {
        return snapshotStoryboard(sbName: sbName, isPortrait: false)
    }
 
    static func changeAllLaunchImageToPortrait(_ image: UIImage?) {
        guard let image = image else {
            return
        }
            // 全部替换为竖屏启动图
        let resizedImage = resizeImage(image, toPortraitScreenSize: true)
        BBADynamicLaunchImage.replaceLaunchImage(resizedImage)
    }
 
    static func changeAllLaunchImageToLandscape(_ image: UIImage?) {
        guard let image = image else {
            return
        }
            // 全部替换为横屏启动图
        let resizedImage = resizeImage(image, toPortraitScreenSize: false)
        BBADynamicLaunchImage.replaceLaunchImage(resizedImage)
    }
 
    static func changePortraitLaunchImage(_ p: UIImage) {
            // Implementation for this function is missing
    }
 
    static func resizeImage(_ image: UIImage, toPortraitScreenSize: Bool) -> UIImage {
            // Implementation for this function is missing
        return image
    }
}
 
 
private class BBADynamicLaunchImage {
    static func launchImageCacheDirectory() -> String? {
        let bundleID = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String
        let fm = FileManager.default
 
            // iOS13之前
        if var cachesDirectory = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first {
            cachesDirectory.append(contentsOf: "Snapshots")
            cachesDirectory.append(contentsOf: bundleID!)
            if fm.fileExists(atPath: cachesDirectory) {
                return cachesDirectory
            }
        }
 
            // iOS13
        if let libraryDirectory = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first {
            let snapshotsPath = String(format: "%@/SplashBoard/Snapshots/%@ - {DEFAULT GROUP}", libraryDirectory, bundleID ?? "")
            if fm.fileExists(atPath: snapshotsPath) {
                return snapshotsPath
            }
        }
 
        return nil
    }
 
    static func isSnapShotName(_ name: String) -> Bool {
            // 新系统后缀
        let snapshotSuffixs = ".ktx"
        if name.hasSuffix(snapshotSuffixs) {
            return true
        }
 
            // 老系统后缀
        let snapshotSuffixs2 = ".png"
        if name.hasSuffix(snapshotSuffixs2) {
            return true
        }
 
        return false
    }
 
    @discardableResult
    static func replaceLaunchImage(_ replacementImage: UIImage?) -> Bool {
        guard let image = replacementImage else {return false}
        return self.replaceLaunchImage(replacementImage: image, compressionQuality: 0.8, customValidation: nil)
    }
 
    @discardableResult
    static func replaceLaunchImage(_ replacementImage: UIImage?, compressionQuality: CGFloat) -> Bool {
        guard let image = replacementImage else {return false}
        return self.replaceLaunchImage(replacementImage: image, compressionQuality: compressionQuality, customValidation: nil)
    }
 
    static func replaceLaunchImage(replacementImage: UIImage, compressionQuality: CGFloat, customValidation: ((UIImage,UIImage) -> Bool)?) -> Bool {
 
        let data = replacementImage.jpegData(compressionQuality: compressionQuality)
        if data == nil {
            return false
        }
 
            //        if !checkImageMatchScreenSize(image: replacementImage) {
            //            return false
            //        }
 
        guard let cacheDir = launchImageCacheDirectory() else {
            return false
        }
 
        let cachesParentDir = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
        let tmpDir = (cachesParentDir as NSString).appendingPathComponent("_tmpLaunchImageCaches")
 
        let fm = FileManager.default
        if fm.fileExists(atPath: tmpDir) {
            do {
                try fm.removeItem(atPath: tmpDir)
            } catch {
                return false
            }
        }
 
        do {
            try fm.moveItem(atPath: cacheDir, toPath: tmpDir)
        } catch {
            return false
        }
 
        var cacheImageNames = [String]()
        if let contents = try? fm.contentsOfDirectory(atPath: tmpDir) {
            for name in contents {
                if isSnapShotName(name) {
                    cacheImageNames.append(name)
                }
            }
        }
 
        for name in cacheImageNames {
            let filePath = (tmpDir as NSString).appendingPathComponent(name)
            var result = true
 
            if customValidation != nil{
                if let cachedImageData = try? Data(contentsOf: URL(string: filePath)!),let cachedImage = imageFromData(data: cachedImageData as NSData){
                    result = customValidation!(cachedImage,replacementImage)
                }
            }
 
            if result {
                do {
                    try data?.write(to: URL(fileURLWithPath: filePath), options: .atomic)
                } catch {
                    return false
                }
            }
        }
 
 
        try? fm.moveItem(atPath: tmpDir, toPath: cacheDir)
        if fm.fileExists(atPath: tmpDir) {
            do {
                try fm.removeItem(atPath: tmpDir)
            } catch {
                return false
            }
        }
 
        return true
    }
 
 
 
    static func imageFromData(data: NSData) -> UIImage? {
        guard let source = CGImageSourceCreateWithData(data, nil) else {
            return nil
        }
 
        if let imageRef = CGImageSourceCreateImageAtIndex(source, 0, nil) {
            let originImage = UIImage(cgImage: imageRef)
            return originImage
        }
 
        return nil
    }
 
    func getImageSize(imageData: NSData) -> CGSize {
        guard let source = CGImageSourceCreateWithData(imageData, nil) else {
            return CGSize.zero
        }
 
        if let imageRef = CGImageSourceCreateImageAtIndex(source, 0, nil) {
            let width = CGFloat(imageRef.width)
            let height = CGFloat(imageRef.height)
            return CGSize(width: width, height: height)
        }
 
        return CGSize.zero
    }
 
 
        /// 检查图片大小
    static func checkImageMatchScreenSize(image: UIImage) -> Bool {
        let screenSize = CGSize(width: UIScreen.main.bounds.size.width * UIScreen.main.scale,
                                height: UIScreen.main.bounds.size.height * UIScreen.main.scale)
        let imageSize = CGSize(width: image.size.width * image.scale,
                               height: image.size.height * image.scale)
 
        if imageSize.equalTo(screenSize) {
            return true
        }
 
        if imageSize.equalTo(CGSize(width: screenSize.height, height: screenSize.width)) {
            return true
        }
 
        return false
    }
}