自动调整“非视网膜"图像版本的大小 [英] Automatic resizing for 'non-retina' image versions

查看:33
本文介绍了自动调整“非视网膜"图像版本的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种解决方案,可以让我免于维护同一图像的两个版本,一个用于 Retina 显示器(又名 @2x),另一个用于非 Retina 显示器.我的目标是仅保留2x"图像,并在 XCode 中构建时通过单击甚至更好地调整所有魔法工具"的大小.就像设置并忘记它".

I'm looking for a solution that can save me from maintaining two versions of the same image, one for Retina displays (aka @2x), one another for non-Retina displays. My goal is to maintain the "2x" images only, and have some 'magic tool' resize all of them with a single click or even better upon building in XCode. Like "set it and forget it".

你能帮我吗?提前致谢.

Can you help me? Thanks in advance.

推荐答案

如果您只是想缩小它们的尺寸,您可以让 Xcode 在构建过程中自动生成所有非视网膜图像.此示例脚本使用sips",因为它已预装在 Mac 上.

If you just want to downscale them, you can have Xcode automatically generate all non-retina images during the build process. This example script uses "sips" because that is preinstalled on Macs.

#!/bin/bash
# Downsamples all retina ...@2x.png images.

echo "Downsampling retina images..."

dir=$(pwd)
find "$dir" -name "*@2x.png" | while read image; do

    outfile=$(dirname "$image")/$(basename "$image" @2x.png).png

    if [ "$image" -nt "$outfile" ]; then
        basename "$outfile"

        width=$(sips -g "pixelWidth" "$image" | awk 'FNR>1 {print $2}')
        height=$(sips -g "pixelHeight" "$image" | awk 'FNR>1 {print $2}')
        sips -z $(($height / 2)) $(($width / 2)) "$image" --out "$outfile"

        test "$outfile" -nt "$image" || exit 1
    fi
done

自动执行

  • 创建一个新的聚合目标",将其命名为图像下采样".
  • 向这个运行脚本的目标添加一个运行脚本"阶段.
  • 在您的应用目标中添加下采样图像"目标作为目标依赖项".
  • 记住仍然将您的 1x 图像添加到 Xcode 项目中.根据您的需要,您可能还想:

    Remember to still add your 1x images to the Xcode project. Depending on your needs you might also want to:

    • 从转换中排除某些文件
    • 将生成的文件添加到您的 .gitignore 文件中
    • 使用 ImageMagick 的convert"而不是sips".(对于某些索引的 PNG,sip 似乎失败.)
    • 运行 optipng
    • exclude certain files from conversion
    • add the generated files to your .gitignore file
    • use ImageMagick's "convert" instead of "sips". (sips seems to fail for some indexed PNGs.)
    • run optipng

    ImageMagick 带有比较"命令,如果您想检查下采样版本.

    ImageMagick comes with a "compare" command if you want to check the downsampled versions.

    这篇关于自动调整“非视网膜"图像版本的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆