From e8f095f33c39204963ebe74bced8d955f8176688 Mon Sep 17 00:00:00 2001 From: Tom Harrington Date: Sat, 2 Jun 2012 15:59:26 -0600 Subject: [PATCH] Add fixpng.sh for convenient fixing of iOS-optimized PNGs. --- README.md | 6 ++++-- fixpng.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 fixpng.sh diff --git a/README.md b/README.md index 08fbd3c..fef1b0d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Atomic Tools -A collection of useful Objective-C code. Most of this has been blogged about at [Cocoa is my Girlfriend](http://www.cimgf.com/). +A collection of useful Objective-C code and other items of use to iOS and Mac OS X developers. Most of this has been blogged about at [Cocoa is my Girlfriend](http://www.cimgf.com/). * `NSObject+setValuesForKeysWithJSONDictionary.h`: Safer alternative to `setValuesForKeysWithDictionary:` for use when importing JSON. Works with any object with declared properties corresponding to JSON dictionary keys. Detailed discussion is at [CIMGF](http://www.cimgf.com/2012/01/11/handling-incoming-json-redux/). @@ -8,6 +8,8 @@ A collection of useful Objective-C code. Most of this has been blogged about at * `NSData+reallyMapped.h`: Create an NSData object using a memory mapped file. Works even though `dataWithContentsOfMappedFile:` is deprecated in iOS 5.0 and `NSDataReadingMappedAlways` doesn't always (despite the name) create memory mapped instances. Described in detail at [CIMGF](http://www.cimgf.com/2012/02/17/extending-nsdata-and-not-overriding-dealloc/). +* `fixpng.sh`: Two shell functions useful for converting iOS-optimized PNGs back into standard PNGs. These functions rely on the `xcrun` command-line tool and probably require that Xcode 4.3 or higher be installed. + # Important note -All code in this repository is designed for use with automated reference counting (ARC). If you are not using ARC you may experience memory leaks or worse. +All Objective-C code in this repository is designed for use with automated reference counting (ARC). If you are not using ARC you may experience memory leaks or worse. diff --git a/fixpng.sh b/fixpng.sh new file mode 100644 index 0000000..3c915bd --- /dev/null +++ b/fixpng.sh @@ -0,0 +1,40 @@ +# Fix an iOS-converted PNG +# By Tom Harrington, June 1 2012. + +fixpng () { + if [ -z "$1" ]; then + echo "Usage: fixpng [outputFile]" + return -1 + else + inputFile=$1 + + # Only "png" and "PNG" are allowed + pngRegex='.*.(png|PNG)$' + if [[ $inputFile =~ $pngRegex ]]; then + if [ -n "$2" ]; then + # Use whatever name was provided + outputFile=$2 + else + # Generate a filename, preserve file extension case. + extension=${BASH_REMATCH[1]} + outputFile=${inputFile%.$extension}-fixed.$extension + fi + echo "Converting $inputFile to $outputFile" + + xcrun -sdk iphoneos pngcrush -q -revert-iphone-optimizations $inputFile $outputFile + else + echo "Skipping $inputFile since it's not a png" + fi + fi +} + +# Fix a whole mess of pngs at once +fixpngs () { + if [ -z "$1" ]; then + echo "Usage: fixpng [outputFile]" + return -1 + else + for file in "$@"; do fixpng $file; done + fi + +}