Xcode15部分Assets图片alpha渲染成灰色

213 阅读1分钟

背景

以前在xcode14上去开发,UI切直接在zeplin直接下载,在Assets使用,在苹果必须让使用xcode15打包之后部分图片alpha渲染成了灰色。

上图对比

Xcode14

image.png

Xcode15

image.png

解决方案

将 png 转换为 8位

脚本

安装Pillow

pip install pillow

Python脚本

from PIL import Image 
# 打开原始图片 
original_image = Image.open("path_to_your_image.jpg") 
# 将图片转换为8位图像(调色板模式) 
converted_image = original_image.convert("RGBA") 
# 保存转换后的图片 
converted_image.save("path_to_save_converted_image.png")

批量处理Assets文件

import os
from PIL import Image


def deal_png_at_location(root):
    for root, dirs, files in os.walk(root):
        for file in files:
            if file.endswith(".png"):
                img_path = os.path.join(root, file)
                img = Image.open(img_path)
                if img.mode != "P":
                    continue
                img = img.convert("RGBA")
                img.save(img_path)

deal_png_at_location("./Assets.xcassets")

完活

把重新生成的图片导入到Assets即可

思路来源 developer.apple.com/forums/thre…