xcode 自动调整“非视网膜”图像版本的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8087997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:23:20  来源:igfitidea点击:

Automatic resizing for 'non-retina' image versions

xcode

提问by gd1

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".

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

Can you help me? Thanks in advance.

你能帮助我吗?提前致谢。

回答by nschum

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.

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

The Script

剧本

#!/bin/bash
# Downsamples all retina [email protected] 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 }')
        height=$(sips -g "pixelHeight" "$image" | awk 'FNR>1 {print }')
        sips -z $(($height / 2)) $(($width / 2)) "$image" --out "$outfile"

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

Automatic Execution

自动执行

  • Create a new "Aggregate Target", name it "Downsample images".
  • Add a "Run script" phase to this target that runs the script.
  • Add the "Downsample images" target as a "Target Dependency" in your app target(s).
  • 创建一个新的“聚合目标”,将其命名为“下采样图像”。
  • 向这个运行脚本的目标添加一个“运行脚本”阶段。
  • 在您的应用程序目标中添加“下采样图像”目标作为“目标依赖项”。

Notes

笔记

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

请记住仍然将您的 1x 图像添加到 Xcode 项目中。根据您的需要,您可能还想:

  • 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
  • 从转换中排除某些文件
  • 将生成的文件添加到您的 .gitignore 文件中
  • 使用 ImageMagick 的“转换”而不是“sips”。(对于某些索引的 PNG,sip 似乎失败了。)
  • 运行优化

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

如果您想检查下采样版本,ImageMagick 带有“比较”命令。

回答by F.X.

This is quite an old thread, but I stumbled onto it, so I can elaborate on maintaining more than one size automatically.

这是一个相当古老的线程,但我偶然发现了它,因此我可以详细说明自动维护多个尺寸。

Performance-wise, I'm not sure using the automatic downscaling is a wise idea, as it has to be done in real-time, but it should work on simpler cases.

在性能方面,我不确定使用自动缩小是一个明智的想法,因为它必须实时完成,但它应该适用于更简单的情况。

Now, to convert these @2ximages automatically, a simple bash script should do the trick. l4u's solution works, but for guys with simpler needs who do not want to install guard, this also works (you still need to install ImageMagick, though) :

现在,要@2x自动转换这些图像,一个简单的 bash 脚本应该可以解决问题。l4u的解决方案有效,但对于那些不想安装但需求更简单的人来说guard,这也有效(不过,您仍然需要安装 ImageMagick):

for f in $(find . -name '*@2x.png'); do
    echo "Converting $f..."
    convert "$f" -resize '50%' "$(dirname $f)/$(basename -s '@2x.png' $f).png"
done

回答by amattn

It's trivial:

这是微不足道的:

  1. Only include @2x images in your project.
  2. Make sure those images have the @2x suffix.
  1. 仅在您的项目中包含 @2x 图像。
  2. 确保这些图像具有 @2x 后缀。

The system will automatically downscale for non-retina devices.

对于非视网膜设备,系统将自动缩小规模。

The only exception is if you are doing manual, low level Core Graphics drawing. You need to adjust the scale if so. 99.9% though, you don't have to worry about this.

唯一的例外是如果您正在进行手动的低级核心图形绘图。如果是这样,您需要调整比例。99.9% 不过,你不必担心这个。

回答by l4u

I've created http://l4u.github.com/blog/2012/04/02/resizing-retina-images-with-guard-for-cocos2d-iphone-slash-cocos2d-x/to generate non-hd images on the fly when -hd images are created/updated. It uses guard, guard-shell and imagemagick

我创建了http://l4u.github.com/blog/2012/04/02/resizing-retina-images-with-guard-for-cocos2d-iphone-slash-cocos2d-x/来生成非高清图像创建/更新 -hd 图像时即时。它使用守卫、守卫壳和 imagemagick

You can replace -hd with @2x.

您可以用@2x 替换-hd。

回答by lara1435

u can simply use only *@2x.png images for your app. but you must set the content mode = UIViewContentModeAspectfit for the imageviews, then it will automatically fix the image to the releventimageviews.

你可以简单地为你的应用只使用 *@2x.png 图像。但是您必须为图像视图设置内容模式 = UIViewContentModeAspectfit,然后它会自动将图像固定到相关图像视图。

回答by SimplyKiwi

Or what you can also do is: Have only the @2x images in your app's bundle then on the first launch. Take all the @2x photos and downsize them by 1/2 and store them in the documents directory. Then when you need your photos for a UIImageView say, just grab them for the documents directory and set it to your UIImageView using code and not Interface Builder!

或者你也可以做的是:在你的应用程序包中只有@2x 图像,然后在第一次启动时。拍摄所有@2x 照片并将它们缩小 1/2 并将它们存储在文档目录中。然后,当您需要为 UIImageView 拍摄照片时,只需将它们抓取到文档目录中,然后使用代码而不是 Interface Builder将其设置为您的 UIImageView

I was wondering this a few weeks ago too and I realized that this is pretty much the only way to really do what you are looking for!

几周前我也想知道这个问题,我意识到这几乎是真正做你正在寻找的事情的唯一方法!

回答by pchap10k

Try Resource Helper on the Mac App Store http://itunes.apple.com/us/app/resourcehelper/id521474600

在 Mac App Store 上试用 Resource Helper http://itunes.apple.com/us/app/resourcehelper/id521474600

It costs $10.49 but it's worth it. Checks if your images are Retina friendly (i.e. even numbered width/height dimensions) and generates the corresponding image inline. Also handles creation of ~ipad and @2x~ipad graphics as needed.

售价 10.49 美元,但物有所值。检查您的图像是否是 Retina 友好的(即偶数宽度/高度尺寸)并生成相应的内联图像。还可以根据需要处理 ~ipad 和 @2x~ipad 图形的创建。

EDIT: I am not affiliated with the authors of Resource Helper.

编辑:我不隶属于 Resource Helper 的作者。

回答by mikermcneil

What I've been doing for our applications is asking our designer to export everything twice as big as it needs to be, then running a little node script to resize the images(anything named @2x in the directory where you run the script). Presently, we're just running the script when every time we deploy (it's idempotent), but it could easily be incorporated into forever -w or some other file-change-watching library like guard.

我一直在为我们的应用程序做的是要求我们的设计师将所有内容导出为所需大小的两倍,然后运行一个小节点脚本来调整图像大小(在您运行脚本的目录中名为 @2x 的任何内容)。目前,我们只是在每次部署时运行脚本(它是幂等的),但它可以很容易地合并到 forever -w 或其他一些文件更改监视库中,例如guard

回答by Ryan McGrath

Old thread, but I found a use for @nschum's script - I noticed though that it doesn't round numbers for the @1x images if it's dividing an odd number. If I recall correctly this introduces a performance hit; wound up slightly revising it as below. Alters the awk call slightly to do the division there and round it accordingly, and won't re-create @1x images if one already exists (you might want to remove that, dunno).

旧线程,但我发现了@nschum 脚本的用途 - 我注意到,如果它除以奇数,它不会舍入 @1x 图像的数字。如果我没记错的话,这会导致性能下降;最后稍微修改如下。稍微改变 awk 调用以在那里进行除法并相应地对其进行舍入,并且如果已经存在则不会重新创建 @1x 图像(您可能想删除它,不知道)。

At this point we've pretty much hit the point where non-retina isn't a big deal (the iPad 2 is all that remains...? The original Mini too, I guess), so meh. Throwing it up for posterity.

在这一点上,我们几乎已经达到了非视网膜不是什么大不了的地步(iPad 2 就是剩下的……?原来的 Mini 也是,我猜),所以meh。把它扔给后代。

#!/bin/bash
# Downsamples all retina [email protected] images.

dir=$(pwd)
echo "Downsampling Retina images..."

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

    if [ "$image" -nt "$outfile" ] && [ ! -f "$outfile" ]; then
        if [[ $(dirname "$image") =~ "Images.xcassets" ]]; then
            echo "Skipping Image XCode Assets directory"
        else
            basename "$outfile"
            width=$(sips -g "pixelWidth" "$image" | awk 'FNR>1 {printf "%.0f\n", /2}')
            height=$(sips -g "pixelHeight" "$image" | awk 'FNR>1 {printf "%.0f\n", /2}')
            sips -z "$height" "$width" "$image" --out "$outfile"
            test "$outfile" -nt "$image" || exit 1
        fi
    fi
done

echo "Finished downsampling Retina images"