Skip to content

Commit aed7bd9

Browse files
chore(preprod): Save images as original file format to ParsedAssets
1 parent b6d72df commit aed7bd9

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

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

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ enum AssetUtil {
7777

7878
let (structuredThemeStore, assetKeys) = initializeCatalog(from: file)
7979

80-
var images: [String: CGImage] = [:]
80+
var images: [String: (cgImage: CGImage, format: String)] = [:]
8181

8282
for key in assetKeys {
8383
let keyList = unsafeBitCast(
@@ -129,7 +129,15 @@ enum AssetUtil {
129129
let (width, height, unslicedImage) = resolveImageDimensions(rendition, isVector)
130130
let assetType = determineAssetType(key)
131131
let imageId = UUID().uuidString
132-
images[imageId] = unslicedImage
132+
133+
// Get format from filename extension, default to PNG if missing
134+
let fileExtension = (name as NSString).pathExtension.lowercased()
135+
let originalFormat = fileExtension.isEmpty ? "png" : fileExtension
136+
137+
// Store CGImage for non-SVG images (SVGs are included in JSON but not saved to disk)
138+
if originalFormat != "svg", let unslicedImage = unslicedImage {
139+
images[imageId] = (cgImage: unslicedImage, format: originalFormat)
140+
}
133141

134142
let asset = AssetCatalogEntry(
135143
imageId: imageId,
@@ -161,17 +169,13 @@ enum AssetUtil {
161169
.appendingPathComponent("Assets")
162170
.appendingPathExtension("json")
163171
try! data.write(to: url, options: [])
164-
for (id, cgImage) in images {
165-
let fileURL = folder.appendingPathComponent(id)
166-
.appendingPathExtension("png")
167-
168-
guard let dest = CGImageDestinationCreateWithURL(
169-
fileURL as CFURL,
170-
UTType.png.identifier as CFString,
171-
1,
172-
nil
173-
)
174-
else {
172+
for (id, imageInfo) in images {
173+
let format = imageInfo.format
174+
let cgImage = imageInfo.cgImage
175+
let fileURL = folder.appendingPathComponent(id).appendingPathExtension(format)
176+
177+
let utType = utTypeForExtension(format)
178+
guard let dest = CGImageDestinationCreateWithURL(fileURL as CFURL, utType as CFString, 1, nil) else {
175179
print("⚠️ Could not create destination for \(fileURL.path)")
176180
continue
177181
}
@@ -293,6 +297,31 @@ enum AssetUtil {
293297

294298
return (width, height, unslicedImage)
295299
}
300+
301+
private static func utTypeForExtension(_ ext: String) -> String {
302+
switch ext {
303+
case "jpg", "jpeg":
304+
return UTType.jpeg.identifier
305+
case "png":
306+
return UTType.png.identifier
307+
case "heic", "heif":
308+
return UTType.heic.identifier
309+
case "gif":
310+
return UTType.gif.identifier
311+
case "webp":
312+
return UTType.webP.identifier
313+
case "pdf":
314+
return UTType.pdf.identifier
315+
case "svg":
316+
return UTType.svg.identifier
317+
case "tiff", "tif":
318+
return UTType.tiff.identifier
319+
case "bmp":
320+
return UTType.bmp.identifier
321+
default:
322+
return UTType.png.identifier
323+
}
324+
}
296325
}
297326

298327
private extension NSObject {

0 commit comments

Comments
 (0)