iOS App

iOS App For Myself.

ImageOptim

ImageOptim

ImageOptim通过寻找最好的压缩参数和移除不必要的commentscolor profiles来减少图片大小,提高加载速度。

ImageOptim可以处理PNG,JPEG和GIF动画。

ImageOptim无缝集成以下优化工具:

ImageOptim代码是开源的,其源代码在GitHub上。

PNG that works文章中解释了为什么ImageOptim移除gamma信息,和怎么在 保留透明度的情况下获取更小的png图片。

Xcode内置的图片优化 pngcrush

Xcode默认会将所有的PNG图片转换成一个非标准的iOS特定的PNG格式CgBI file format

这种格式使用premultiplied BGRA代替了RGBA颜色空间,会在loading阶段节省一点点转换时间,但完全不影响 图片渲染(Rendering)速度。

Xcode的这种转换有时会使图片增大,会抵消ImageOptim的优化。

可以在Build Settings中将Compress PNG Files设为NO来禁止这种转换。

Apple的PNG变体,在iOS外无法使用,也无法使用Preview工具查看,可以使用pngcrush工具进行逆向转换成正常的png图片。

pngcrush在Xcode的安装目录中存在,路径类似为/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/pngcrush

1
$ /Applications/Xcode.app/Contents/Developer\/Platforms/iPhoneOS.platform/Developer/usr/bin/pngcrush \-revert-iphone-optimizations -q Local.png Local-standard.png

可将Local.png 恢复成正常的Local-standard.png

Image Alpha

ImageAlpha通过有损压缩和转换为更有效率的PNG8+alpha格式,可以有效的减小24-bit PNG文件(包括alpha透明度)的大小。其生成的图片兼容iOS,所有的浏览器设置IE6。

ImageAlpha通过使用最新的pngquant,pngnq-s9alpha-channel-awareposterizer 能达到比Adobe Fireworks中类似功能更好的图片质量。

ImageAlpha基于pngquant,在ImageAlpha.app/Contents/Resources目录下会有pngquant执行文件,可以用于批量处理。ImageOptim-CLI也会使用此文件。

ImageAlpha是开源的,其代码是python的。source code在github上。

JPEGmini for Mac

JPEGmini Mac版本可以对JPEG图片最高进行5倍的压缩,并保持原始的质量和JPEG格式。

JPEGmini是收费软件,在appstore有一个免费版本JPEGmini Lite下载,免费版本每天只能压缩20张图片。

ImageOptim-CLI

ImageOptim-CLI命令行工具使用ImageOptim,ImageAlpha,JPEGmini for Mac 对图片进行批量优化。

从目前的benchmarks可看出ImageOptim,ImageAlpha和JPEGmini的优化效果比其他替代品要好。

下载最新的zip包,解压并将ImageOptim-CLI的路径添加到$PATH中。

1
2
3
$ curl --output imageoptim-cli.zip https://codeload.github.com/JamieMason/ImageOptim-CLI/zip/1.7.11
$ unzip imageoptim-cli.zip
$ export PATH=$PATH:imageoptim-cli/bin

使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  Usage: imageOptim [options]

  Options:

  -d, --directory     directory of images to process
  -a, --image-alpha   pre-process PNGs with ImageAlpha.app *
  -j, --jpeg-mini     pre-process JPGs with JPEGmini.app **
  -q, --quit          quit all apps when complete
  -h, --help          display this usage information
  -e, --examples      display some example commands and uses
  -v, --version       display the version number

*  http://pngmini.com
** https://itunes.apple.com/us/app/jpegmini/id498944723
  • -d指定要优化的图片文件目录;
  • -a,-j指定预先使用ImageAlpha优化PNG图片和使用JPEGmini优化JPEG图片,因为JPEGmini为收费的,如果没有 安装,不指定-j即可;

一些示例命令

1
2
3
4
imageOptim --directory ~/Sites/Awesome # [options]
find ~/Sites/Awesome -name '*.jpg' | imageOptim # [options]
imageOptim --jpeg-mini --image-alpha --quit --directory path/to/images
imageOptim -j -a -q -d path/to/images