Skip to content

Commit 0ac1433

Browse files
chore(preprod): Add AssetCatalog Parsing support for Multisize sets
1 parent 56c0024 commit 0ac1433

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

apple-catalog-parsing/native/swift/AssetCatalogParser/Sources/AssetCatalogParser/AssetCatalogReader.swift

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,23 @@ enum AssetUtil {
153153
let type = rendition.getUInt(forKey: "type") ?? 0
154154

155155
let isVector = type == 9
156-
let (width, height, unslicedImage) = resolveImageDimensions(rendition, isVector)
156+
var (width, height, unslicedImage) = resolveImageDimensions(rendition, isVector)
157157
let assetType = determineAssetType(key)
158158
let imageId = UUID().uuidString
159-
160159
let fileExtension = (renditionTypeName as NSString).pathExtension.lowercased()
160+
161+
if unslicedImage == nil && className == "_CUIThemeMultisizeImageSetRendition" {
162+
if let result = findImageForMultisizeSet(rendition, key, assetKeys, structuredThemeStore) {
163+
unslicedImage = result.image
164+
width = result.width
165+
height = result.height
166+
}
167+
}
161168

162-
// Skip files without an extension or SVGs
163-
if !fileExtension.isEmpty && fileExtension != "svg", let unslicedImage = unslicedImage {
164-
images[imageId] = (cgImage: unslicedImage, format: fileExtension)
169+
// Skip SVGs, but save images even if they don't have an extension (default to png)
170+
if fileExtension != "svg", let unslicedImage = unslicedImage {
171+
let format = fileExtension.isEmpty ? "png" : fileExtension
172+
images[imageId] = (cgImage: unslicedImage, format: format)
165173
}
166174

167175
let idiomValue = key.getUInt(forKey: "themeIdiom")
@@ -276,6 +284,60 @@ enum AssetUtil {
276284
}
277285
return true
278286
}
287+
288+
private static func findImageForMultisizeSet(
289+
_ rendition: NSObject,
290+
_ key: NSObject,
291+
_ assetKeys: [NSObject],
292+
_ structuredThemeStore: NSObject
293+
) -> (image: CGImage, width: Int, height: Int)? {
294+
// Get the sizeIndexes to find the actual image
295+
guard rendition.responds(to: Selector(("sizeIndexes"))),
296+
let sizeIndexesResult = rendition.perform(Selector(("sizeIndexes"))),
297+
let sizeIndexesArray = sizeIndexesResult.takeUnretainedValue() as? NSArray,
298+
sizeIndexesArray.count > 0 else {
299+
return nil
300+
}
301+
302+
// Get the first size index
303+
let sizeIndexObj = sizeIndexesArray.object(at: 0) as! NSObject
304+
305+
// Get the idiom and subtype from the size index
306+
let idiom = sizeIndexObj.getUInt(forKey: "idiom") ?? 0
307+
let subtype = sizeIndexObj.getUInt(forKey: "subtype") ?? 0
308+
let keyElement = key.getUInt(forKey: "themeElement") ?? 0
309+
310+
// Look for a rendition with matching idiom and subtype in the asset keys
311+
for otherKey in assetKeys {
312+
let otherKeyIdiom = otherKey.getUInt(forKey: "themeIdiom") ?? 0
313+
let otherKeySubtype = otherKey.getUInt(forKey: "themeSubtype") ?? 0
314+
let otherKeyElement = otherKey.getUInt(forKey: "themeElement") ?? 0
315+
316+
// Find a key with matching element, idiom, and subtype
317+
guard otherKeyElement == keyElement,
318+
otherKeyIdiom == idiom,
319+
otherKeySubtype == subtype else {
320+
continue
321+
}
322+
323+
let otherKeyList = unsafeBitCast(
324+
otherKey.perform(Selector(("keyList"))),
325+
to: UnsafeMutableRawPointer.self
326+
)
327+
328+
guard let imageRendition = createRendition(from: structuredThemeStore, otherKeyList),
329+
let className = imageRendition.perform(Selector(("className")))?.takeUnretainedValue() as? String,
330+
className != "_CUIThemeMultisizeImageSetRendition",
331+
let result = imageRendition.perform(Selector(("unslicedImage"))) else {
332+
continue
333+
}
334+
335+
let image = result.takeUnretainedValue() as! CGImage
336+
return (image, image.width, image.height)
337+
}
338+
339+
return nil
340+
}
279341

280342
private static func determineAssetType(_ key: NSObject) -> AssetType {
281343
let themeElement = key.getUInt(forKey: "themeElement") ?? 0

0 commit comments

Comments
 (0)