Automatic resizing for 'non-retina' image versions

前端 未结 9 1524
生来不讨喜
生来不讨喜 2021-01-30 15:03

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

相关标签:
9条回答
  • 2021-01-30 15:41

    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.

    0 讨论(0)
  • 2021-01-30 15:43

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

    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.

    #!/bin/bash
    # Downsamples all retina ...@2x.png 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/2}')
                height=$(sips -g "pixelHeight" "$image" | awk 'FNR>1 {printf "%.0f\n", $2/2}')
                sips -z "$height" "$width" "$image" --out "$outfile"
                test "$outfile" -nt "$image" || exit 1
            fi
        fi
    done
    
    echo "Finished downsampling Retina images"
    
    0 讨论(0)
  • 2021-01-30 15:44

    Try Resource Helper on the Mac App Store 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.

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

    0 讨论(0)
提交回复
热议问题